STL algorithm is_permutation (27), stlalgorithm

Source: Internet
Author: User

STL algorithm is_permutation (27), stlalgorithm

Is_permutation prototype:

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

This function is used to determine whether two sequences are arranged differently in the same element set!

This function uses operator = or pred to determine whether the two elements are equal.

Its behavior is similar:

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

The code block in windows does not support this function. I am sweating!

A simple test example:

#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;     }
Running result:


We can see that although v3 has one element 1, v1 and v3 are different in the same element set!

The data type of v4 is double!




------------------------------------------------------------------

// For more instructions on writing errors or poor information, you can leave a message below or click the email address in the upper left corner to send an email to me, pointing out my errors and deficiencies, so that I can modify them, thank you for sharing it.

Reprinted please indicate the source: http://blog.csdn.net/qq844352155

Author: unparalleled

Email: coderguang@gmail.com

Yu GDUT

------------------------------------------------------------------





The algorithm function in C ++ STL can count the number of occurrences of a character in a string. How can this function be used?

Char s [] = "hello stl ";
Int n = count (s, s + 10, 'L'); // calculates the number of characters 'l' in string s. The first two parameters represent the interval to be searched, and the third parameter represents the value to be searched. The function returns the number of times the specified value appears in the interval. If the specified value is not included in the interval, 0 is returned.
 
The third parameter question about for_each () of STL algorithm

......, Print <int> );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.