STL算法总结

 

 

 

 

//  AC one more times  #include<iostream> #include<string> #include<map> #include<queue> #include<random> #include<time.h> #include<algorithm> #include<functional> #include<numeric>  using namespace std; bool cmp1(int i, int j) { return (i > j); }//nth_element  int main() {     srand(time(0));     vector<int> a, b;     for (int i = 1; i <= 10; i++)     {         int t = rand() % 10 + 1;         a.push_back(t);         b.push_back(t);      }      //遍历方法     cout << 遍历方法 << endl;     for (vector<int>::iterator iter = a.begin(); iter != a.end(); iter++)     {         cout << *iter <<  ;     }     puts();     for (auto& it : a)     {         cout << it <<  ;     }     puts();      //  find函数     cout << find函数使用 << endl;     int value = rand() % 10 + 1;     puts();     cout << value << endl;     if (find(a.begin(), a.end(), value) != a.end())         cout << found << endl;     puts(); puts();      //reverse翻转数组,字符串     cout << reverse翻转数组,字符串 << endl;     reverse(a.begin(), a.end());     for (auto& it : a)         cout << it <<  ;     puts();     //翻转一部分     cout << reverse翻转翻转一部分 << endl;     reverse(a.begin(), a.begin() + 2);     for (auto& it : a)         cout << it <<  ;     puts(); puts();        //unique 去重     //要先排序     cout << unique 去重,要先排序 << endl;     sort(a.begin(), a.end());     a.erase(unique(a.begin(), a.end()), a.end());     for (auto& it : a)         cout << it <<  ;     puts(); puts();       for (int i = a.size(); i <= 10; i++)     {         int t = rand() % 10 + 1;         a.push_back(t);     }     for (auto& it : a)         cout << it <<  ;     puts();      //stable_sort:稳定排序,用法同 sort()     cout << stable_sort:稳定排序,用法同 sort() << endl;     stable_sort(a.begin(), a.end());     for (auto& it : a)         cout << it <<  ;     puts(); puts();      /*     nth_element:按指定范围进行分类,即找出序列中第n大的元素,      使其左边均为小于它的数,右边均为大于它的数。      nth_element(v.begin(), v.begin() + mid, v.end(), cmp)     或 nth_element(a + begin, a + begin + mid, a + end, cmp)      */     cout << nth_element:按指定范围进行分类,即找出序列中第n大的元素, << endl;     cout << 使其左边均为小于它的数,右边均为大于它的数。 << endl;   puts();      cout << nth_element(v.begin(), v.begin() + mid, v.end(), cmp)  << endl;     cout << 或 nth_element(a + begin, a + begin + mid, a + end, cmp) << endl;     nth_element(a.begin(), a.begin() + 5, a.end(), cmp1);     for (auto& it : a)         cout << it <<  ;     puts(); puts();      cout << 也可以nth_element(a.begin(), a.begin() + 5, a.end()); << endl;     nth_element(a.begin(), a.begin() + 5, a.end());     for (auto& it : a)         cout << it <<  ;     puts(); puts();      //binary_search:二分查找。     //binary_search(v.begin(), v.end(), value),其中 value 为需要查找的值。     //返回值是0或1,bool     cout << binary_search:二分查找。 << endl;     cout << binary_search(v.begin(), v.end(), value),其中 value 为需要查找的值。 << endl;     cout << 返回值是0或1,bool << endl;     sort(a.begin(), a.end());     int v = 5;    cout << 查找值:   << v << endl;     if (binary_search(a.begin(), a.end(), v) == true)         cout << found << endl;     else cout << no found << endl;     puts(); puts();      //lower_bound:在一个有序序列中进行二分查找,     //返回指向第一个 大于等于  的元素的位置的迭代器。     //如果不存在这样的元素,则返回尾迭代器。lower_bound(v.begin(),v.end(),x)。     cout << lower_bound:在一个有序序列中进行二分查找, << endl;     cout << 返回指向第一个 大于等于  的元素的位置的迭代器。 << endl;     cout << 如果不存在这样的元素,则返回尾迭代器。lower_bound(v.begin(),v.end(),x)。 << endl;     for (auto& it : a)         cout << it <<  ;     puts();     int t = lower_bound(a.begin(), a.end(), v) - a.begin();     cout << v <<        << t << endl;     puts(); puts();        sort(a.begin(), a.end());     sort(b.begin(), b.end());     //inplace_merge:将两个(已按小于运算符排序的):[first,middle), [middle,last)      //范围 原地合并为一个有序序列。     //inplace_merge(v.begin(), v.begin() + middle, v.end())。     cout << inplace_merge:将两个(已按小于运算符排序的):[first,middle), [middle,last)  << endl;     cout << 范围 原地合并为一个有序序列。 << endl;     cout << inplace_merge(v.begin(), v.begin() + middle, v.end())。 << endl;     //inplace_merge(a.begin(), a.begin() + middle, a.end())   /* next_permutation:将当前排列更改为 全排列中的下一个排列。如果当前排列已经是 全排列中的最后一个排列(元素完全从大到小排列),函数返回 false 并将排列更改为 全排列中的第一个排列(元素完全从小到大排列);否则,函数返回 true。next_permutation(v.begin(), v.end()) 或 next_permutation(v + begin, v + end)。 partial_sum:求前缀和。设源容器为 ,目标容器为 ,则令 。partial_sum(src.begin(), src.end(), back_inserter(dst))。  */           return 0;     //STL容器共有:     /*     =:有赋值运算符以及复制构造函数。      begin():返回指向开头元素的迭代器。      end():返回指向末尾的下一个元素的迭代器。end() 不指向某个元素,但它是末尾元素的后继。      size():返回容器内的元素个数。      max_size():返回容器 理论上 能存储的最大元素个数。依容器类型和所存储变量的类型而变。      empty():返回容器是否为空。      swap():交换两个容器。      clear():清空容器。      ==/!=/</>/<=/>=:按 字典序 比较两个容器的大小。(比较元素大小时 map 的每个元素相当于 set<pair<key, value> >,无序容器不支持 </>/<=/>=。)      */  }