zoukankan      html  css  js  c++  java
  • STL 算法

    一.STL算法分类
    STL中的算法大致可以分为以下七类:
    不变序列算法
    变值算法
    删除算法
    变序算法
    排序算法
    有序区间算法
    数值算法
    2
    大多重载的算法都是有
    两个版本
    “==” 判断元素是否相等, 或用 “<” 来比较大小
    多出一个类型参数 Pred和函数形参 Pred op” :通过表达式 “op(x,y)” 的返回值: ture/false判断x是否 等于” y,或者x是否 小于” y
    如下面的有两个版本的min_element:
    iterator min_element(iterator first, iterator last);
    iterator min_element(iterator first, iterator last, Pred op);
    3
    1. 不变序列算法
    该类算法不会修改算法所作用的容器或对象
    适用于顺序容器和关联容器
    时间复杂度都是O(n)

    min 求两个对象中较小的(可自定义比较器)
    max 求两个对象中较大的(可自定义比较器)
    min_element 求区间中的最小值(可自定义比较器)
    max_element 求区间中的最大值(可自定义比较器)
    for_each 对区间中的每个元素都做某种操作
    count 计算区间中等于某值的元素个数
    count_if 计算区间中符合某种条件的元素个数
    find 在区间中查找等于某值的元素
    find_if 在区间中查找符合某条件的元素
    find_end 在区间中查找另一个区间最后一次出现的位置(可自定义比较器)
    find_first_of 在区间中查找第一个出现在另一个区间中的元素 (可自定义比较器)
    adjacent_find 在区间中寻找第一次出现连续两个相等元素的位置(可自定义比较器)
    search 在区间中查找另一个区间第一次出现的位置
    (可自定义比较器)自定义比较器)
    search_n 在区间中查找第一次出现等于某值的连续n个元素
    (可自定义比较器)
    素(可自定义比较器)equal 判断两区间是否相等(可自定义比较器)
    mismatch 逐个比较两个区间的元素,返回第一次发生不相等的两个元素的位置(可自定义比较器)

    相等的两个元素的位置(可自定义比较器)lexicographical_compare 按字典序比较两个区间的大小(可自定义比较器)


    find:
    template<class InIt, class T>
    InIt find(InIt first, InIt last, const T& val);
    返回区间 [first,last) 中的迭代器 i ,使得 * i == val


    find_if:
    template<class InIt, class Pred>
    InIt find_if(InIt first, InIt last, Pred pr);
    返回区间 [first,last) 中的迭代器 i, 使得 pr(*i) == true
    7
    for_each:
    template<class InIt, class Fun>
    Fun for_each(InIt first, InIt last, Fun f);
    对[first, last)中的每个元素e, 执行f(e), 要求 f(e)不能改变e
    8
    count:
    template<class InIt, class T>
    size_t count(InIt first, InIt last, const T& val);
    计算[first, last) 中等于val的元素个数(x==y为true算等于)
    count_if:
    template<class InIt, class Pred>
    size_t count_if(InIt first, InIt last, Pred pr);
    计算[first, last) 中符合pr(e) == true 的元素e的个数
    9
    min_element:
    template<class FwdIt>
    FwdIt min_element(FwdIt first, FwdIt last);
    返回[first,last) 中最小元素的迭代器, 以 “<” 作比较器
    最小指没有元素比它小, 而不是它比别的不同元素都小
    因为即便a!= b, a<b 和b<a有可能都不成立


    max_element:
    template<class FwdIt>
    FwdIt max_element(FwdIt first, FwdIt last);
    返回[first,last) 中最大元素(不小于任何其他元素)的迭代器
    “<” 作比较器,以 “<” 作比较器

     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 class A {
     5 public:
     6     int n;
     7     A(int i):n(i) { }
     8 };
     9 bool operator<( const A & a1, const A & a2) {
    10     cout << "< called" << endl;
    11     if( a1.n == 3 && a2.n == 7 )
    12         return true;
    13     return false;
    14 }
    15 
    16 int main() {
    17     A aa[] = { 3,5,7,2,1 };
    18     cout << min_element(aa,aa+5)->n << endl;
    19     cout << max_element(aa,aa+5)->n << endl;
    20     return 0;
    21 }
    22 
    23 //输出:
    24 //< called
    25 //< called
    26 //< called
    27 //< called
    28 //3
    29 //< called
    30 //< called
    31 //< called
    32 //< called
    33 //< called
    34 //7

    2. 变值算法
    此类算法会修改源区间或目标区间元素的值
    值被修改的那个区间, 不可以是属于关联容器的
    for_each 对区间中的每个元素都做某种操作
    copy 复制一个区间到别处
    copy_backward 复制一个区间到别处, 但目标区前是从后往前
    被修改的
    transform 将一个区间的元素变形后拷贝到另一个区间
    swap_ranges 交换两个区间内容
    fill 用某个值填充区间
    fill_n 用某个值替换区间中的n个元素
    generate 用某个操作的结果填充区间
    generate_n 用某个操作的结果替换区间中的n个元素
    replace 将区间中的某个值替换为另一个值
    replace_if 将区间中符合某种条件的值替换成另一个值
    replace_copy 将一个区间拷贝到另一个区间,拷贝时某个值
    要换成新值拷过去
    replace_copy_if 将一个区间拷贝到另一个区间,拷贝时符合某
    条件的值要换成新值拷过去


    transform
    template<class InIt, class OutIt, class Unop>
    OutIt transform(InIt first, InIt last, OutIt x, Unop uop);
    对[first,last)中的每个迭代器I,
    执行 uop( * I ); 并将结果依次放入从 x 开始的地方
    要求 uop( * I ) 不得改变 * I 的值
    本模板返回值是个迭代器, 即 x + (last-first)
    ; x可以和 first相等

     1 #include <vector>
     2 #include <iostream>
     3 #include <numeric>
     4 #include <list>
     5 #include <algorithm>
     6 #include <iterator>
     7 using namespace std;
     8 class CLessThen9 {
     9 public:
    10     bool operator()( int n) { return n < 9; }
    11 };
    12 void outputSquare(int value ) { cout << value * value << " "; }
    13 int calculateCube(int value) { return value * value * value; }
    14 
    15 int main() {
    16     const int SIZE = 10;
    17     int a1[] = { 1,2,3,4,5,6,7,8,9,10 };
    18     int a2[] = { 100,2,8,1,50,3,8,9,10,2 };
    19     vector<int> v(a1,a1+SIZE);
    20     ostream_iterator<int> output(cout," ");
    21     random_shuffle(v.begin(),v.end());
    22     cout << endl << "1) ";
    23     copy( v.begin(),v.end(),output);
    24     copy( a2,a2+SIZE,v.begin());
    25     cout << endl << "2)";
    26     cout << count(v.begin(),v.end(),8);
    27     cout << endl << "3)";
    28     cout << count_if(v.begin(),v.end(),CLessThen9());
    29 
    30 //输出:
    31 //    1) 9 2 10 3 1 6 8 4 5 7
    32 //    2) 2
    33 //    3) 6
    34 //    //1) 是随机的
    35     cout << endl << "4) ";
    36     cout << * (min_element(v.begin(), v.end()));
    37     cout << endl << "5) ";
    38     cout << * (max_element(v.begin(), v.end()));
    39     cout << endl << "6) ";
    40     cout << accumulate(v.begin(), v.end(), 0); //求和
    41 
    42 //输出:
    43 //    4) 1
    44 //    5) 100
    45 //    6) 193
    46     cout << endl << "7) ";
    47     for_each(v.begin(), v.end(), outputSquare);
    48     vector<int> cubes(SIZE);
    49     transform(a1, a1+SIZE, cubes.begin(), calculateCube);
    50     cout << endl << "8) ";
    51     copy(cubes.begin(), cubes.end(), output);
    52     return 0;
    53 }
    54 
    55 //输出:
    56 //7)10000 4 64 1 2500 9 64 81 100 4
    57 //8)1 8 27 64 125 216 343 512 729 1000

    ostream_iterator<int> output(cout ,“ ”);
    定义了一个 ostream_iterator<int> 对象,
    可以通过cout输出以 “ ”(空格) 分隔的一个个整数
    copy (v.begin(), v.end(), output);
    导致v的内容在 cout上输出

    template<class InIt, class OutIt>
    OutIt copy(InIt first, InIt last, OutIt x);
    本函数对每个在区间[0, last - first)中的N执行一次
    *(x+N) = *(first + N), 返回 x + N
    对于copy(v.begin(),v.end(),output);
    first 和 last 的类型是 vector<int>::const_iterator
    output 的类型是 ostream_iterator<int>
    21
    copy 函数模板(算法)
    copy 的源代码:
    template<class _II, class _OI>
    inline _OI copy(_II _F, _II _L, _OI _X)
    {
           for (; _F != _L; ++_X, ++_F)
           *_X = *_F;
           return (_X);
    }

     1 #include <iostream>
     2 #include <fstream>
     3 #include <string>
     4 #include <algorithm>
     5 #include <iterator>
     6 using namespace std;
     7 
     8 template<class T>
     9 class My_ostream_iterator:public iterator<output_iterator_tag, T>{
    10 private:
    11     string sep; //分隔符
    12     ostream & os;
    13 public:
    14     My_ostream_iterator(ostream & o, string s):sep(s), os(o){ }
    15     void operator ++() { }; // ++只需要有定义即可, 不需要做什么
    16     My_ostream_iterator & operator * () { return * this; }
    17     My_ostream_iterator & operator = ( const T & val)
    18     { os << val << sep; return * this; }
    19 }; 
    20 
    21 int main(){
    22     int a[4] = { 1,2,3,4 };
    23     My_ostream_iterator<int> oit(cout,"*");
    24     copy(a,a+4,oit); //输出 1*2*3*4*
    25     ofstream oFile("test.txt", ios::out);
    26     My_ostream_iterator<int> oitf(oFile,"*");
    27     copy(a,a+4,oitf); //向test.txt文件中写入 1*2*3*4*
    28     oFile.close();
    29     return 0;
    30 }
    31 // 如何编写 My_ostream_iterator? 
    32 // 上面程序中调用语句 “copy( a,a+4,oit)” 实例化后得到copy如下:
    33 My_ostream_iterator<int> copy(int * _F, int * _L, My_ostream_iterator<int> _X)
    34 {
    35     for (; _F != _L; ++_X, ++_F)
    36         *_X = *_F;
    37     return (_X);
    38 }
    39 //copy 的源代码:
    40 template<class _II, class _OI>
    41 inline _OI copy(_II _F, _II _L, _OI _X){
    42     for (; _F != _L; ++_X, ++_F)
    43         *_X = *_F;
    44     return (_X);
    45 }

    3. 删除算法
    删除一个容器里的某些元素
    删除 -- 不会使容器里的元素减少
    将所有应该被删除的元素看做空位子
    用留下的元素从后往前移, 依次去填空位子
    元素往前移后, 它原来的位置也就算是空位子
    也应由后面的留下的元素来填上
    最后, 没有被填上的空位子, 维持其原来的值不变
    删除算法不应作用于关联容器
    26
    remove 删除区间中等于某个值的元素
    remove_if 删除区间中满足某种条件的元素
    remove_copy 拷贝区间到另一个区间. 等于某个值的元素不拷贝
    remove_copy_if 拷贝区间到另一个区间. 符合某种条件的元素不拷贝
    unique 删除区间中连续相等的元素, 只留下一个(可自定义比较器)
    unique_copy 拷贝区间到另一个区间. 连续相等的元素, 只拷贝第一个到目标区间 (可自定义比较器)
    算法复杂度都是O(n)的


    unique
    template<class FwdIt>
    FwdIt unique(FwdIt first, FwdIt last);
    用 == 比较是否等
    template<class FwdIt, class Pred>
    FwdIt unique(FwdIt first, FwdIt last, Pred pr);
    用 pr (x,y)为 true说明x和y相等
    对[first,last) 这个序列中连续相等的元素, 只留下第一个返回值是迭代器, 指向元素删除后的区间的最后一个元素的后面

     1 #include <iostream>
     2 #include <vector>
     3 #include <string>
     4 #include <algorithm>
     5 #include <iterator>
     6 using namespace std;
     7 
     8 int main(){
     9     int a[5] = { 1,2,3,2,5 };
    10     int b[6] = { 1,2,3,2,5,6 };
    11     ostream_iterator<int> oit(cout,",");
    12     int * p = remove(a,a+5,2);
    13     cout << "1) "; copy(a,a+5,oit); cout << endl; //输出 1) 1,3,5,2,5,
    14     cout << "2) " << p - a << endl; //输出 2) 3
    15     vector<int> v(b,b+6);
    16     remove(v.begin(), v.end(),2);
    17     cout << "3) "; copy(v.begin(), v.end(), oit); cout << endl;
    18     //输出 3) 1,3,5,6,5,6,
    19     cout << "4) "; cout << v.size() << endl;
    20     //v中的元素没有减少,输出 4) 6
    21     return 0;
    22 }

    4. 变序算法
    变序算法改变容器中元素的顺序,但是不改变元素的值
    变序算法不适用于关联容器
    算法复杂度都是O(n)的
    30
    算法名称 功 能
    reverse 颠倒区间的前后次序
    reverse_copy 把一个区间颠倒后的结果拷贝到另一个区间,源区间不变
    rotate 将区间进行循环左移
    rotate_copy 将区间以首尾相接的形式进行旋转后的结果拷贝到另一个区间,源区间不变
    next_permutation 将区间改为下一个排列(可自定义比较器)
    prev_permutation 将区间改为上一个排列(可自定义比较器)
    random_shuffle 随机打乱区间内元素的顺序
    partition 把区间内满足某个条件的元素移到前面,不满足该条件的移到后面条件的移到后面条件的移到后面条件的移到后面条件的移到后面


    stable_patition
    把区间内满足某个条件的元素移到前面
    不满足该条件的移到后面
    而对这两部分元素, 分别保持它们原来的先后次序不变


    random_shuffle
    template<class RanIt>
    void random_shuffle(RanIt first, RanIt last);
    随机打乱[first,last) 中的元素, 适用于能随机访问的容器
    32
    reverse
    template<class BidIt>
    void reverse(BidIt first, BidIt last);
    颠倒区间[first,last)顺序


    next_permutation
    template<class InIt>
    bool next_permutaion (Init first,Init last);
    求下一个排列

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string>
     4 using namespace std;
     5 int main(){
     6     string str = "231";
     7     char szStr[] = "324";
     8     while (next_permutation(str.begin(), str.end())){
     9         cout << str << endl;
    10     }
    11     cout << "****" << endl;
    12     while (next_permutation(szStr,szStr + 3)){
    13         cout << szStr << endl;
    14     }
    15 //输出:
    16 //    312
    17 //    321
    18 //    ****
    19 //    342
    20 //    423
    21 //    432
    22     sort(str.begin(), str.end());
    23     cout << "****" << endl;
    24     while (next_permutation(str.begin(), str.end()))
    25     {
    26         cout << str << endl;
    27     }
    28     return 0;
    29 }
    30 
    31 //输出:
    32 //132
    33 //213
    34 //231
    35 //312
    36 //321
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string>
     4 #include <list>
     5 #include <iterator>
     6 using namespace std;
     7 int main(){
     8     int a[] = { 8,7,10 };
     9     list<int> ls(a, a+3);
    10     while( next_permutation(ls.begin(), ls.end())) {
    11         list<int>::iterator i;
    12         for( i = ls.begin(); i != ls.end(); ++i)
    13             cout << * i << " ";
    14         cout << endl;
    15     }
    16 }
    17 
    18 //输出:
    19 //8 10 7
    20 //10 7 8
    21 //10 8 7

    5.排序算法
    比前面的变序算法复杂度更高, 一般是O(nlog(n))
    排序算法需要随机访问迭代器的支持
    不适用于关联容器和list

    sort 将区间从小到大排序(可自定义比较器)

    stable_sort 将区间从小到大排序并保持相等元素间的相对次序(可自定义比较器)

    partial_sort 对区间部分排序, 直到最小的n个元素就位(可自定义比较器)

    partial_sort_copy 将区间前n个元素的排序结果拷贝到别处源区间不变(可自定义比较器)

    nth_element 对区间部分排序, 使得第n小的元素(n从0开始算)就位, 而且比它小的都在它前面, 比它大的都在它后面(可自定义比较器)

    make_heap 使区间成为一个“堆” (可自定义比较器)

    push_heap 将元素加入一个是“堆”区间(可自定义比较器)

    pop_heap 从“ 堆” 区间删除堆顶元素(可自定义比较器)

    sort_heap 将一个“堆”区间进行排序,排序结束后,该区间就是普通的有序区间,不再是 “堆”了(可自定义比较器)

    sort 快速排序 

    template<class RanIt>void sort(RanIt first, RanIt last);按升序排序判断x是否应比y靠前, 就看 x < y 是否为true

    template<class RanIt, class Pred>void sort(RanIt first, RanIt last, Pred pr);按升序排序判断x是否应比y靠前, 就看 pr(x,y) 是否为true

     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 
     5 class MyLess {
     6 public:
     7     bool operator()( int n1,int n2) {
     8         return (n1 % 10) < ( n2 % 10);
     9     }
    10 };
    11 
    12 //按个位数大小排序,
    13 //按降序排序
    14 //输出:
    15 //111 2 14 78 9
    16 //111 78 14 9 2
    17 int main() {
    18     int a[] = { 14,2,9,111,78 };
    19     sort(a, a + 5, MyLess());
    20     int i;
    21     for( i = 0;i < 5;i ++)
    22         cout << a[i] << " ";
    23     cout << endl;
    24     //sort(a, a+5, greater<int>());
    25     //for( i = 0;i < 5;i ++)
    26     //    cout << a[i] << " ";
    27 }

    sort 实际上是快速排序, 时间复杂度 O(n*log(n))
    平均性能最优
    但是最坏的情况下, 性能可能非常差
    如果要保证 最坏情况下的性能, 那么可以使用
    stable_sort
    stable_sort 实际上是归并排序, 特点是能保持相等元素之间的先后次序
    在有足够存储空间的情况下, 复杂度为 n * log(n), 否则复杂度为 n * log(n) * log(n)
    stable_sort 用法和 sort相同。
    排序算法要求随机存取迭代器的支持, 所以list不能使用排序算法, 要使用list::sort

    6. 有序区间算法
    要求所操作的区间是已经从小到大排好序的
    需要随机访问迭代器的支持
    有序区间算法不能用于关联容器和list
    42
    binary_search 判断区间中是否包含某个元素
    includes 判断是否一个区间中的每个元素,都在另一个区间中
    lower_bound 查找最后一个不小于某值的元素的位置
    upper_bound 查找第一个大于某值的元素的位置
    equal_range 同时获取lower_boundupper_bound
    merge 合并两个有序区间到第三个区间
    算法名称 功能
    set_union 将两个有序区间的并拷贝到第三个区间
    set_intersection 将两个有序区间的交拷贝到第三个区间
    set_difference 将两个有序区间的差拷贝到第三个区间
    set_symmetric_difference 将两个有序区间的对称差拷贝到第三个区间
    inplace_merge 将两个连续的有序区间原地合并为一个有序区间
    43
    binary_search
    折半查找
    要求容器已经有序且支持随机访问迭代器, 返回是否找到
    template<class FwdIt, class T>
    bool binary_search(FwdIt first, FwdIt last, const T& val);
    上面这个版本, 比较两个元素x, y 大小时, x < y
    template<class FwdIt, class T, class Pred>
    bool binary_search(FwdIt first, FwdIt last, const T& val, Pred pr);
    上面这个版本, 比较两个元素x, y 大小时, pr(x,y) true, 则认为x小于y

     1 #include <vector>
     2 #include <bitset>
     3 #include <iostream>
     4 #include <numeric>
     5 #include <list>
     6 #include <algorithm>
     7 using namespace std;
     8 bool Greater10(int n)
     9 {
    10     return n > 10;
    11 }
    12 
    13 int main() {
    14     const int SIZE = 10;
    15     int a1[] = { 2,8,1,50,3,100,8,9,10,2 };
    16     vector<int> v(a1,a1+SIZE);
    17     ostream_iterator<int> output(cout," ");
    18     vector<int>::iterator location;
    19     location = find(v.begin(),v.end(),10);
    20     if( location != v.end()) {
    21         cout << endl << "1) " << location - v.begin();
    22     }
    23     location = find_if( v.begin(),v.end(),Greater10);
    24     if( location != v.end())
    25         cout << endl << "2) " << location - v.begin();
    26     sort(v.begin(),v.end());
    27     if( binary_search(v.begin(),v.end(),9)) {
    28         cout << endl << "3) " << "9 found";
    29     }
    30 }
    31 
    32 //输出:
    33 //1) 8
    34 //2) 3
    35 //3) 9 found

     
    lower_bound
    template<class FwdIt, class T>
    FwdIt lower_bound(FwdIt first, FwdIt last, const T& val);
    要求[first,last)是有序的
    查找[first,last)中的, 最大的位置 FwdIt, 使得[first,FwdIt)
    中所有的元素都比 val 小
    48
    upper_bound
    template<class FwdIt, class T>
    FwdIt upper_bound(FwdIt first, FwdIt last, const T& val);
    要求[first,last)是有序的
    查找[first,last)中的, 最小的位置 FwdIt, 使得[FwdIt,last)
    中所有的元素都比 val 大
    49
    equal_range
    template<class FwdIt, class T>
    pair<FwdIt, FwdIt> equal_range(FwdIt first, FwdIt last,
    const T& val);
    要求[first,last)是有序的,
    返回值是一个pair, 假设为 p, 则:
    [first,p.first) 中的元素都比 val 小
    [p.second,last)中的所有元素都比 val 大
    p.first 就是lower_bound的结果
    p.last 就是 upper_bound的结果

    50
    merge
    template<class InIt1, class InIt2, class OutIt>
    OutIt merge(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2,
    OutIt x);
    用 < 作比较器
    template<class InIt1, class InIt2, class OutIt, class Pred>
    OutIt merge(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2,
    OutIt x, Pred pr);
    用 pr 作比较器
    把[first1,last1), [ first2,last2) 两个升序序列合并, 形成第
    3 个升序序列, 第3个升序序列以 x 开头
    51
    includes
    template<class InIt1, class InIt2>
    bool includes(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2);
    template<class InIt1, class InIt2, class Pred>
    bool includes(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2,
    Pred pr);
    判断 [first2,last2)中的每个元素, 是否都在[first1,last1)中
    第一个用 <作比较器
    第二个用 pr 作比较器, pr(x,y) == true说明 x,y相等
    52
    set_difference
    template<class InIt1, class InIt2, class OutIt>
    OutIt set_difference(InIt1 first1, InIt1 last1, InIt2 first2,
    InIt2 last2, OutIt x);
    template<class InIt1, class InIt2, class OutIt, class Pred>
    OutIt set_difference(InIt1 first1, InIt1 last1, InIt2 first2,
    InIt2 last2, OutIt x, Pred pr);
    求出[first1,last1)中, 不在[first2,last2)中的元素, 放到 从 x
    开始的地方
    如果 [first1,last1) 里有多个相等元素不在[first2,last2)中,
    则这多个元素也都会被放入x代表的目标区间里
    53
    set_intersection
    template<class InIt1, class InIt2, class OutIt>
    OutIt set_intersection(InIt1 first1, InIt1 last1, InIt2 first2,
    InIt2 last2, OutIt x);
    template<class InIt1, class InIt2, class OutIt, class Pred>
    OutIt set_intersection(InIt1 first1, InIt1 last1, InIt2 first2,
    InIt2 last2, OutIt x, Pred pr);
    求出[first1,last1)和[first2,last2)中共有的元素, 放到从x开始的
    地方
    若某个元素e 在[first1,last1)里出现 n1次, 在[first2,last2)里出
    现n2次, 则该元素在目标区间里出现min(n1,n2)次
    54
    set_symmetric_difference
    template<class InIt1, class InIt2, class OutIt>
    OutIt set_symmetric_difference(InIt1 first1, InIt1 last1,
    InIt2 first2, InIt2 last2, OutIt x);
    template<class InIt1, class InIt2, class OutIt, class Pred>
    OutIt set_symmetric_difference(InIt1 first1, InIt1 last1,
    InIt2 first2, InIt2 last2, OutIt x, Pred pr);
    把两个区间里相互不在另一区间里的元素放入x开始的
    地方
    55
    set_union
    template<class InIt1, class InIt2, class OutIt>
    OutIt set_union(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2,
    OutIt x);
    用<比较大小
    template<class InIt1, class InIt2, class OutIt, class Pred>
    OutIt set_union(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2,
    OutIt x, Pred pr);
    用 pr 比较大小
    求两个区间的并, 放到以 x开始的位置
    若某个元素e 在[first1,last1)里出现 n1次, 在[first2,last2)里
    出现n2次, 则该元素在目标区间里出现max(n1,n2)次

    56
    bitset
    template<size_t N>
    class bitset
    {
    ..
    };
    实际使用的时候, N是个整型常数
    :
    bitset<40> bst;
    bst是一个由40位组成的对象
    bitset的函数可以方便地访问任何一位
    57
    bitset的成员函数:
    bitset<N>& operator&=(const bitset<N>& rhs);
    bitset<N>& operator|=(const bitset<N>& rhs);
    bitset<N>& operator^=(const bitset<N>& rhs);
    bitset<N>& operator<<=(size_t num);
    bitset<N>& operator>>=(size_t num);
    bitset<N>& set(); //全部设成1
    bitset<N>& set(size_t pos, bool val = true); //设置某位
    bitset<N>& reset(); //全部设成0
    bitset<N>& reset(size_t pos); //某位设成0
    bitset<N>& flip(); //全部翻转
    bitset<N>& flip(size_t pos); //翻转某位
    58
    reference operator[](size_t pos); //返回对某位的引用
    bool operator[](size_t pos) const; //判断某位是否为1
    reference at(size_t pos);
    bool at(size_t pos) const;
    unsigned long to_ulong() const; //转换成整数
    string to_string() const; //转换成字符串
    size_t count() const; //计算1的个数
    size_t size() const;
    bool operator==(const bitset<N>& rhs) const;
    bool operator!=(const bitset<N>& rhs) const;
    59
    bool test(size_t pos) const; //测试某位是否为 1
    bool any() const; //是否有某位为1
    bool none() const; //是否全部为0
    bitset<N> operator<<(size_t pos) const;
    bitset<N> operator>>(size_t pos) const;
    bitset<N> operator~();
    static const size_t bitset_size = N;
    注意: 第0位在最右边
    to_string() const; //转换成字符串size_t count() const; //计算1的个数size_t size() const;bool operator==(const bitset<N>& rhs) const;bool operator!=(const bitset<N>& rhs) const;59bool test(size_t pos) const; //测试某位是否为 1bool any() const; //是否有某位为1bool none() const; //是否全部为0bitset<N> operator<<(size_t pos) const;bitset<N> operator>>(size_t pos) const;bitset<N> operator~();static const size_t bitset_size = N;注意: 第0位在最右边<

    string to_string() const; //转换成字符串

    size_t count() const; //计算1的个数

    size_t size() const;
    bool operator==(const bitset<N>& rhs) const;
    bool operator!=(const bitset<N>& rhs) const;
    59
    bool test(size_t pos) const; //测试某位是否为 1
    bool any() const; //是否有某位为1
    bool none() const; //是否全部为0
    bitset<N> operator<<(size_t pos) const;
    bitset<N> operator>>(size_t pos) const;
    bitset<N> operator~();
    static const size_t bitset_size = N;
    注意: 第0位在最右边

    2

     

     

    自定义比较器)

  • 相关阅读:
    一步步构建大型网站架构
    IIS访问设置
    如何删除windows中的服务
    IIS下对WebConfig加密后无法访问网站
    ConfigurationManager不认的问题
    ORA12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
    PostgreSQL中表和字符串大写的问题
    ASP.NET中注册客户端脚本的三种方式
    装箱(boxing)和拆箱(unboxing)
    windows开启防火墙后IIS下的网站外网无法访问
  • 原文地址:https://www.cnblogs.com/wanderingzj/p/5344961.html
Copyright © 2011-2022 走看看