STL algorithm演算法is_permutation(27)

來源:互聯網
上載者:User

標籤:algorithm   stl   演算法   c++11   

is_permutation原型:

std::is_permutation
equality (1)
template <class ForwardIterator1, class ForwardIterator2>   bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,                        ForwardIterator2 first2);
predicate (2)
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>   bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,                        ForwardIterator2 first2, BinaryPredicate pred);

該函數是用來判斷兩個序列是否為同一元素集的不同排列!

該函數使用operator==或者是pred來判斷兩個元素是否是相等的。

其行為類似:

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;}

坑的是windows下面的codeblock居然不支援這個函數,我汗!

一個簡單的測試例子:

#include <iostream>  #include <vector>  #include <array>  #include <algorithm>  using namespace std;  int main(){      vector<int> v1{1,2,3,4,5,6};      vector<int> v2{1,5,6,4,3,2};      vector<int> v3{1,3,2,5,6,4,1};//add a elements 1      vector<double> v4{1,2,4,3,5,6};  vector<int> v5{1,0,2,3,4,5,6};    array<int,6> ai{6,5,3,4,2,1};        cout<<"v1=";      for(int &i:v1)          cout<<i<<" ";      cout<<endl;       cout<<"v2=";      for(int &i:v2)          cout<<i<<" ";      cout<<endl;     cout<<"v3=";      for(int &i:v3)          cout<<i<<" ";      cout<<endl;     cout<<"v4=";      for(double &i:v4)          cout<<i<<" ";      cout<<endl;   cout<<"v5=";      for(int &i:v5)          cout<<i<<" ";      cout<<endl;   cout<<"ai=";      for(int &i:ai)          cout<<i<<" ";      cout<<endl;      if(is_permutation(v1.begin(),v1.end(),v2.begin()))cout<<"Yes ,v1 and v2 is permutation!"<<endl; elsecout<<"No ,v1 and v2 is not permutation!"<<endl;  if(is_permutation(v1.begin(),v1.end(),v3.begin()))cout<<"Yes ,v1 and v3 is permutation!"<<endl; elsecout<<"No ,v1 and v3 is not permutation!"<<endl; if(is_permutation(v1.begin(),v1.end(),v4.begin()))cout<<"Yes ,v1 and v4 is permutation!"<<endl; elsecout<<"No ,v1 and v4 is not permutation!"<<endl; if(is_permutation(v1.begin(),v1.end(),v5.begin()))cout<<"Yes ,v1 and v5 is permutation!"<<endl; elsecout<<"No ,v1 and v5 is not permutation!"<<endl; if(is_permutation(v1.begin(),v1.end(),ai.begin()))cout<<"Yes ,v1 and ai is permutation!"<<endl; elsecout<<"No ,v1 and ai is not permutation!"<<endl;     }
啟動並執行結果:


可以看到,雖然v3多了一個元素1,但是v1和v3還是屬於同一元素集的不同排列!

v4的資料類型為double,也是一樣!




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

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

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

author:天下無雙

Email:[email protected]

2014-9-17

於GDUT

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




STL algorithm演算法is_permutation(27)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.