美文网首页
C++ STL bit_xor 使用说明

C++ STL bit_xor 使用说明

作者: book_02 | 来源:发表于2020-06-02 19:39 被阅读0次

1. 说明

异或的函数对象类。
可用作 transform 或者 accumulate等的操作符。

类似的还有与操作bit_and 、或操作 bit_or

2. 头文件

#include <functional>

3. 例子:对一个数组逐个进行异或操作

比如常见的问题:一个数组里只有1个数出现1次,其他数都出现2次。
这时可用异或操作找出这个只出现一次的数。

#include <iostream>
#include <numeric>
#include <functional>

using namespace std;

int main()
{
    vector<int> nums = { 1,2,1,3,2 };

    int num = std::accumulate(nums.begin(), nums.end(), 0, std::bit_xor<int>());

    cout << num << endl;

    return 0;
}

结果:

3

这里用了std::accumulate()来简化代码,也可以写成for循环形式。

4. 参考

http://www.cplusplus.com/reference/functional/bit_xor/
https://en.cppreference.com/w/cpp/utility/functional/bit_xor

相关文章

  • C++ STL bit_xor 使用说明

    1. 说明 异或的函数对象类。可用作 transform 或者 accumulate等的操作符。 类似的还有与操作...

  • 读书笔记17.06.03

    C++ STL:Listlist是C++标准模版库(STL,Standard Template Library)中...

  • [C++] STL 容器

    参考:[C++] STL 容器 (一) - 基本介紹[C++] STL 容器 (二) - Iterator 部分示例:

  • C++ STL 学习笔记

    C++ STL 学习笔记

  • STL之参考文献

    C++标准库是离不开模板的,STL占了C++标准库80%以上。 学习STL(c++ 98)的主要参考: gcc 3...

  • C++ STL multiplies 使用说明

    说明 std::multiplies是乘法的二元函数对象。 常被用于std::transform或者std::ac...

  • C++ STL accumulate 使用说明

    说明 std::accumulate 用于对容器[first,last)内的元素和初始值init进行累积运算。 前...

  • C++ STL shuffle 使用说明

    说明 使用一个随机数产生器来打乱[first, last)之间元素的顺序。 有3个参数,前2个参数指定容器的范围,...

  • 任务列表

    C++ 《C++ primer》、《STL源码解析》、《effective C++》、《深度搜索c++对象模型》 ...

  • STL初认识

    一 C++ 与STL 历史 STL全称standard template library,由Alexander S...

网友评论

      本文标题:C++ STL bit_xor 使用说明

      本文链接:https://www.haomeiwen.com/subject/wtypzhtx.html