日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL中算法锦集(四)

發布時間:2024/4/11 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL中算法锦集(四) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

STL中算法錦集(四)

文章目錄

  • STL中算法錦集(四)
    • 一、< algorithm >
      • 1.std::is_permutation
      • 2.std::is_sorted
      • 3.std::is_sorted_until
      • 4.std::iter_swap
      • 5.std::lexicographical_compare
      • 6.std::lower_bound
      • 7.std::make_heap
      • 8.std::max_element

一、< algorithm >

1.std::is_permutation

  • 原型:
template <class ForwardIterator1, class ForwardIterator2>bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2);template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, BinaryPredicate pred);
  • 參數:
  • ForwardIterator1 first1:區間1的開始
  • ForwardIterator1 last1:區間1的結束
  • ForwardIterator2 first2: 區間2的開始
  • BinaryPredicate pred:自定義比較規則,可以是函數指針或者仿函數對象、lambda表達式
  • 作用:用來檢查一個序列是不是另一個序列的排列,
  • 返回值:如果是,會返回 true
  • 實現:
template <class InputIterator1, class InputIterator2>bool is_permutation (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2) {std::tie (first1,first2) = std::mismatch (first1,last1,first2);if (first1==last1) return true;InputIterator2 last2 = first2; std::advance (last2,std::distance(first1,last1));for (InputIterator1 it1=first1; it1!=last1; ++it1) {if (std::find(first1,it1,*it1)==it1) {auto n = std::count (first2,last2,*it1);if (n==0 || std::count (it1,last1,*it1)!=n) return false;}}return true; }
  • 案例:
// is_permutation example #include <iostream> // std::cout #include <algorithm> // std::is_permutation #include <array> // std::arrayint main () {std::array<int,5> foo = {1,2,3,4,5};std::array<int,5> bar = {3,1,4,5,2};if ( std::is_permutation (foo.begin(), foo.end(), bar.begin()) )std::cout << "foo and bar contain the same elements.\n";return 0; }Output: foo and bar contain the same elements.

2.std::is_sorted

  • 原型:
template <class ForwardIterator>bool is_sorted (ForwardIterator first, ForwardIterator last);template <class ForwardIterator, class Compare>bool is_sorted (ForwardIterator first, ForwardIterator last, Compare comp);
  • 參數:
  • ForwardIterator first:區間的開始
  • ForwardIterator last:區間的結束
  • Compare comp:自定義比較規則,可以是函數指針或者仿函數對象、lambda表達式
  • 作用:判斷[first,last)區間內的元素是否滿足comp的排序規則
  • 返回值:是返回true
  • 實現:
template <class ForwardIterator>bool is_sorted (ForwardIterator first, ForwardIterator last) {if (first==last) return true;ForwardIterator next = first;while (++next!=last) {// or, if (comp(*next,*first))if (*next<*first) return false;++first;}return true; }
  • 案例:
// is_sorted example #include <iostream> // std::cout #include <algorithm> // std::is_sorted, std::prev_permutation #include <array> // std::arrayint main () {std::array<int,4> foo {2,4,1,3};do {// try a new permutation:std::prev_permutation(foo.begin(),foo.end());// print range:std::cout << "foo:";for (int& x:foo) std::cout << ' ' << x;std::cout << '\n';} while (!std::is_sorted(foo.begin(),foo.end()));std::cout << "the range is sorted!\n";return 0; }Output: foo: 2 3 4 1 foo: 2 3 1 4 foo: 2 1 4 3 foo: 2 1 3 4 foo: 1 4 3 2 foo: 1 4 2 3 foo: 1 3 4 2 foo: 1 3 2 4 foo: 1 2 4 3 foo: 1 2 3 4 the range is sorted!

3.std::is_sorted_until

  • 原型:
template <class ForwardIterator>ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last);template <class ForwardIterator, class Compare>ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last,Compare comp);
  • 參數:
  • ForwardIterator first:區間的開始
  • ForwardIterator last:區間的結束
  • Compare comp:自定義比較規則,可以是函數指針或者仿函數對象、lambda表達式
  • 作用:能檢測出[first,last)區間是否滿足come規則有序
  • 返回值:還會返回一個正向迭代器,該迭代器指向的是當前序列中第一個破壞有序狀態的元素。
  • 實現:
template <class ForwardIterator>ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last) {if (first==last) return first;ForwardIterator next = first;while (++next!=last) {if (*next<*first) return next;++first;}return last; }
  • 案例:
// is_sorted_until example #include <iostream> // std::cout #include <algorithm> // std::is_sorted_until, std::prev_permutation #include <array> // std::arrayint main () {std::array<int,4> foo {2,4,1,3};std::array<int,4>::iterator it;do {// try a new permutation:std::prev_permutation(foo.begin(),foo.end());// print range:std::cout << "foo:";for (int& x:foo) std::cout << ' ' << x;it=std::is_sorted_until(foo.begin(),foo.end());std::cout << " (" << (it-foo.begin()) << " elements sorted)\n";} while (it!=foo.end());std::cout << "the range is sorted!\n";return 0; }Output: foo: 2 3 4 1 (3 elements sorted) foo: 2 3 1 4 (2 elements sorted) foo: 2 1 4 3 (1 elements sorted) foo: 2 1 3 4 (1 elements sorted) foo: 1 4 3 2 (2 elements sorted) foo: 1 4 2 3 (2 elements sorted) foo: 1 3 4 2 (3 elements sorted) foo: 1 3 2 4 (2 elements sorted) foo: 1 2 4 3 (3 elements sorted) foo: 1 2 3 4 (4 elements sorted) the range is sorted!

4.std::iter_swap

  • 原型:
template <class ForwardIterator1, class ForwardIterator2>void iter_swap (ForwardIterator1 a, ForwardIterator2 b);
  • 參數:
  • ForwardIterator1 a:區間的開始
  • ForwardIterator1 b:區間的結束
  • 作用:交換a,b兩個迭代器的元素內容
  • 返回值:沒有返回值
  • 實現:
template <class ForwardIterator1, class ForwardIterator2>void iter_swap (ForwardIterator1 a, ForwardIterator2 b) {swap (*a, *b); }
  • 案例:
// iter_swap example #include <iostream> // std::cout #include <algorithm> // std::iter_swap #include <vector> // std::vectorint main () {int myints[]={10,20,30,40,50 }; // myints: 10 20 30 40 50std::vector<int> myvector (4,99); // myvector: 99 99 99 99std::iter_swap(myints,myvector.begin()); // myints: [99] 20 30 40 50// myvector: [10] 99 99 99std::iter_swap(myints+3,myvector.begin()+2); // myints: 99 20 30 [99] 50// myvector: 10 99 [40] 99std::cout << "myvector contains:";for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0; }Output: myvector contains: 10 99 40 99

5.std::lexicographical_compare

  • 原型:
template <class InputIterator1, class InputIterator2>bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2);template <class InputIterator1, class InputIterator2, class Compare>bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,Compare comp);
  • 參數:
  • InputIterator1 first1:區間1的開始
  • InputIterator1 last1:區間1的結束
  • InputIterator2 first2:區間2的開始
  • InputIterator2 last2:區間2的結束
  • Compare comp:自定義比較規則,可以是函數指針或者仿函數對象、lambda表達式
  • 作用:求[first1,last1)區間的字典序是否小于[first2,last2)區間的元素
  • 返回值:是就返回true
  • 實現:
template <class InputIterator1, class InputIterator2>bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2) {while (first1!=last1){if (first2==last2 || *first2<*first1) return false;else if (*first1<*first2) return true;++first1; ++first2;}return (first2!=last2); }
  • 案例:
// lexicographical_compare example #include <iostream> // std::cout, std::boolalpha #include <algorithm> // std::lexicographical_compare #include <cctype> // std::tolower// a case-insensitive comparison function: bool mycomp (char c1, char c2) { return std::tolower(c1)<std::tolower(c2); }int main () {char foo[]="Apple";char bar[]="apartment";std::cout << std::boolalpha;std::cout << "Comparing foo and bar lexicographically (foo<bar):\n";std::cout << "Using default comparison (operator<): ";std::cout << std::lexicographical_compare(foo,foo+5,bar,bar+9);std::cout << '\n';std::cout << "Using mycomp as comparison object: ";std::cout << std::lexicographical_compare(foo,foo+5,bar,bar+9,mycomp);std::cout << '\n';return 0; }Output: Comparing foo and bar lexicographically (foo<bar): Using default comparison (operator<): true Using mycomp as comparison object: false

6.std::lower_bound

  • 原型:
template <class ForwardIterator, class T>ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,const T& val);template <class ForwardIterator, class T, class Compare>ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,const T& val, Compare comp);
  • 參數:
  • ForwardIterator first:區間的開始
  • ForwardIterator last:區間的結束
  • const T& val:
  • Compare comp:自定義比較規則,可以是函數指針或者仿函數對象、lambda表達式
  • 作用:在first和last中的前閉后開區間進行二分查找
  • 返回值:返回大于或等于val的第一個元素位置。如果所有元素都小于val,則返回last的位置.
  • 實現:
template <class ForwardIterator, class T>ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val) {ForwardIterator it;iterator_traits<ForwardIterator>::difference_type count, step;count = distance(first,last);while (count>0){it = first; step=count/2; advance (it,step);if (*it<val) { // or: if (comp(*it,val)), for version (2)first=++it;count-=step+1;}else count=step;}return first; }
  • 案例:
// lower_bound/upper_bound example #include <iostream> // std::cout #include <algorithm> // std::lower_bound, std::upper_bound, std::sort #include <vector> // std::vectorint main () {int myints[] = {10,20,30,30,20,10,10,20};std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30std::vector<int>::iterator low,up;low=std::lower_bound (v.begin(), v.end(), 20); // ^up= std::upper_bound (v.begin(), v.end(), 20); // ^std::cout << "lower_bound at position " << (low- v.begin()) << '\n';std::cout << "upper_bound at position " << (up - v.begin()) << '\n';return 0; }Output: lower_bound at position 3 upper_bound at position 6

7.std::make_heap

  • 原型:
template <class RandomAccessIterator>void make_heap (RandomAccessIterator first, RandomAccessIterator last);template <class RandomAccessIterator, class Compare>void make_heap (RandomAccessIterator first, RandomAccessIterator last,Compare comp );
  • 參數:
  • RandomAccessIterator first:區間的開始
  • RandomAccessIterator last:區間的結束
  • Compare comp:自定義比較規則,可以是函數指針或者仿函數對象、lambda表達式
  • 作用:根據[first,last)區間的元素構建一個comp規則的堆

  • 返回值:沒有返回值

  • 案例:

// range heap example #include <iostream> // std::cout #include <algorithm> // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap #include <vector> // std::vectorint main () {int myints[] = {10,20,30,5,15};std::vector<int> v(myints,myints+5);std::make_heap (v.begin(),v.end());std::cout << "initial max heap : " << v.front() << '\n';std::pop_heap (v.begin(),v.end()); v.pop_back();std::cout << "max heap after pop : " << v.front() << '\n';v.push_back(99); std::push_heap (v.begin(),v.end());std::cout << "max heap after push: " << v.front() << '\n';std::sort_heap (v.begin(),v.end());std::cout << "final sorted range :";for (unsigned i=0; i<v.size(); i++)std::cout << ' ' << v[i];std::cout << '\n';return 0; }Edit & RunOutput: initial max heap : 30 max heap after pop : 20 max heap after push: 99 final sorted range : 5 10 15 20 99

8.std::max_element

  • 原型:
template <class ForwardIterator>ForwardIterator max_element (ForwardIterator first, ForwardIterator last);template <class ForwardIterator, class Compare>ForwardIterator max_element (ForwardIterator first, ForwardIterator last,Compare comp);
  • 參數:
  • ForwardIterator first:區間的開始
  • ForwardIterator last:區間的結束
  • Compare comp:自定義比較規則,可以是函數指針或者仿函數對象、lambda表達式
  • 作用:求[first,last)區間內最大的元素
  • 返回值:返回區間內最大的元素的迭代器
  • 實現:
template <class ForwardIterator>ForwardIterator max_element ( ForwardIterator first, ForwardIterator last ) {if (first==last) return last;ForwardIterator largest = first;while (++first!=last)if (*largest<*first) // or: if (comp(*largest,*first)) for version (2)largest=first;return largest; }
  • 案例:
// min_element/max_element example #include <iostream> // std::cout #include <algorithm> // std::min_element, std::max_elementbool myfn(int i, int j) { return i<j; }struct myclass {bool operator() (int i,int j) { return i<j; } } myobj;int main () {int myints[] = {3,7,2,5,6,4,9};// using default comparison:std::cout << "The smallest element is " << *std::min_element(myints,myints+7) << '\n';std::cout << "The largest element is " << *std::max_element(myints,myints+7) << '\n';// using function myfn as comp:std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myfn) << '\n';std::cout << "The largest element is " << *std::max_element(myints,myints+7,myfn) << '\n';// using object myobj as comp:std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myobj) << '\n';std::cout << "The largest element is " << *std::max_element(myints,myints+7,myobj) << '\n';return 0; }Edit & RunOutput: The smallest element is 2 The largest element is 9 The smallest element is 2 The largest element is 9 The smallest element is 2 The largest element is 9

總結

以上是生活随笔為你收集整理的STL中算法锦集(四)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。