STL algorithm演算法includes(23),stlalgorithm

來源:互聯網
上載者:User

STL algorithm演算法includes(23),stlalgorithm

includes原型:

std::includes
template <class InputIterator1, class InputIterator2>  bool includes ( InputIterator1 first1, InputIterator1 last1,                  InputIterator2 first2, InputIterator2 last2 );template <class InputIterator1, class InputIterator2, class Compare>  bool includes ( InputIterator1 first1, InputIterator1 last1,                  InputIterator2 first2, InputIterator2 last2, Compare comp );

該函數是測試  [first1,last1)裡面是否包含了[first2,last2)裡面的所有元素。

也就是說,後面那一個區間是否是前一個區間的子集。

[first1,last1)和[first2,last2)裡面的元素應該先排好序,並且是使用同一規則排序。

其行為類似如下:

234567891011
template <class InputIterator1, class InputIterator2>  bool includes (InputIterator1 first1, InputIterator1 last1,                 InputIterator2 first2, InputIterator2 last2){  while (first2!=last2) {    if ( (first1==last1) || (*first2<*first1) ) return false;    if (!(*first1<*first2)) ++first2;    ++first1;  }  return true;}
 
一個簡單的例子:

#include <iostream>#include <vector>#include <array>#include <algorithm>using namespace std;void includes2(){    vector<int> vi{1,5,8,19,78,100,56};    array<int,3> ai{19,100,56};    vector<int> v2{5,11,20};    sort(vi.begin(),vi.end());    sort(ai.begin(),ai.end());    sort(v2.begin(),v2.end());    cout<<"at first,vi=";    for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});    cout<<endl;    cout<<"at first,ai=";    for_each(ai.begin(),ai.end(),[](int i){cout<<i<<" ";});    cout<<endl;    cout<<"at first,v2=";    for_each(v2.begin(),v2.end(),[](int i){cout<<i<<" ";});    cout<<endl;    if(includes(vi.begin(),vi.end(),ai.begin(),ai.end()))       cout<<"includes(vi.begin(),vi.end(),ai.begin(),ai.end())  return true!"<<endl;    else        cout<<"includes(vi.begin(),vi.end(),ai.begin(),ai.end())  return false!"<<endl;    if(includes(vi.begin(),vi.end(),v2.begin(),v2.end()))        cout<<"includes(vi.begin(),vi.end(),v2.begin(),v2.end()) return true!"<<endl;    else        cout<<"includes(vi.begin(),vi.end(),v2.begin(),v2.end()) return false!"<<endl;}

運行:


如果不排序,則有會發生錯誤!

例如下面的例子:

#include <iostream>#include <vector>#include <array>#include <algorithm>using namespace std;void includes2(){    vector<int> vi{1,5,8,19,78,100,56};    array<int,4> ai{19,100,56,1};    vector<int> v2{5,11,20};   // sort(vi.begin(),vi.end());   // sort(ai.begin(),ai.end());   // sort(v2.begin(),v2.end());    cout<<"at first,vi=";    for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});    cout<<endl;    cout<<"at first,ai=";    for_each(ai.begin(),ai.end(),[](int i){cout<<i<<" ";});    cout<<endl;    cout<<"at first,v2=";    for_each(v2.begin(),v2.end(),[](int i){cout<<i<<" ";});    cout<<endl;    if(includes(vi.begin(),vi.end(),ai.begin(),ai.end()))       cout<<"includes(vi.begin(),vi.end(),ai.begin(),ai.end())  return true!"<<endl;    else        cout<<"includes(vi.begin(),vi.end(),ai.begin(),ai.end())  return false!"<<endl;    if(includes(vi.begin(),vi.end(),v2.begin(),v2.end()))        cout<<"includes(vi.begin(),vi.end(),v2.begin(),v2.end()) return true!"<<endl;    else        cout<<"includes(vi.begin(),vi.end(),v2.begin(),v2.end()) return false!"<<endl;}

運行:




——————————————————————————————————————————————————————————————————

//寫的錯誤或者不好的地方請多多指導,可以在下面留言或者點擊左上方郵件地址給我發郵件,指出我的錯誤以及不足,以便我修改,更好的分享給大家,謝謝。

轉載請註明出處:http://blog.csdn.net/qq844352155

author:天下無雙

Email:coderguang@gmail.com

2014-9-16

於GDUT

——————————————————————————————————————————————————————————————————





STL中的includes

判斷 i和j誰大水小如果i比j小則返回true;
 
4、 下面是對於STL中演算法的部分描述,回答:

1、sorting algorithm的作用,及其包括哪些演算法?
排序演算法把無序狀態的元素排列成你想要的順序,包括快速排序,堆排序等等
2、nth_element功能是什嗎?
按照你指定的定序,找到排出來最後會位於第n位置的元素
3、sort是基於哪個演算法實現的?其average和worst case time complexity是什嗎?
sort是根據快速排序演算法實現的, aver是lgN 最壞是 n^2
4、partial_sort() or stable_sort()與sort演算法的區別?
partial_sor只會排列所有元素中的最前幾個, 具體幾個你指定
stable_sort(會排列所喲的,他保證 如果a=b的情況下, a本來在b的前面,那麼排完序後a還是在b的前面, partial_sor 就不保證,所以他排完序之後有可能會倒個
sort就是普通的全部排列,同時不保證次序維持原來的
5、請列舉STL演算法中的sorting algorithm有哪些?

包括快速排序,堆排序等等
參考資料:自己的腦袋
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.