STL algorithm search, search_n (52)

Source: Internet
Author: User

Search prototype:

STD: Search
Equality (1)
template <class ForwardIterator1, class ForwardIterator2>   ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,                            ForwardIterator2 first2, ForwardIterator2 last2);
Predicate (2)
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>   ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,                            ForwardIterator2 first2, ForwardIterator2 last2,                            BinaryPredicate pred);
This function is used to find the location where [first2, last2) appears for the first time in [first1, last1.

It is equivalent to the position where a subsequence first appears in a sequence.

If the match succeeds, the first matched element in [first1, last1) is returned.

Otherwise, last1.

The behavior is similar:

template<class ForwardIterator1, class ForwardIterator2>  ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,                            ForwardIterator2 first2, ForwardIterator2 last2){  if (first2==last2) return first1;  // specified in C++11    while (first1!=last1)  {    ForwardIterator1 it1 = first1;    ForwardIterator2 it2 = first2;    while (*it1==*it2) {    // or: while (pred(*it1,*it2)) for version 2        ++it1; ++it2;        if (it2==last2) return first1;        if (it1==last1) return last1;    }    ++first1;  }  return last1;}

A simple example:

#include <iostream>#include <algorithm>#include <vector>using namespace std;void msearch(){    char vc[]={"iamastudentstuden"};    char vt[]={"student"};    auto it=search(vc,vc+17,vt,vt+7);    cout<<"auto it=search(vc,vc+17,vt,vt+7);;"<<endl;    for(auto i=it;i!=vc+17;++i)        cout<<*i;    cout<<endl;}
Run:





Search_n prototype:

STD: search_n
Equality (1)
template <class ForwardIterator, class Size, class T>   ForwardIterator search_n (ForwardIterator first, ForwardIterator last,                             Size count, const T& val);
Predicate (2)
template <class ForwardIterator, class Size, class T, class BinaryPredicate>   ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,                              Size count, const T& val, BinaryPredicate pred );
This function is used to find the position where consecutive count Val values appear for the first time in the sequence.

If yes, the first element that appears for the first time is returned. Otherwise, the last element is returned.

The behavior is similar:

template<class ForwardIterator, class Size, class T>  ForwardIterator search_n (ForwardIterator first, ForwardIterator last,                            Size count, const T& val){  ForwardIterator it, limit;  Size i;  limit=first; std::advance(limit,std::distance(first,last)-count);  while (first!=limit)  {    it = first; i=0;    while (*it==val)       // or: while (pred(*it,val)) for the pred version      { ++it; if (++i==count) return first; }    ++first;  }  return last;}
A simple example:

#include <iostream>#include <algorithm>#include <vector>using namespace std;bool mypredicate (int i, int j) {  return (i==j);}void msearchn(){    int myints[]={10,20,30,30,20,10,10,20};  std::vector<int> myvector (myints,myints+8);  std::vector<int>::iterator it;  // using default comparison:  it = std::search_n (myvector.begin(), myvector.end(), 2, 30);  if (it!=myvector.end())    std::cout << "two 30s found at position " << (it-myvector.begin()) << '\n';  else    std::cout << "match not found\n";  // using predicate comparison:  it = std::search_n (myvector.begin(), myvector.end(), 2, 10, mypredicate);  if (it!=myvector.end())    std::cout << "two 10s found at position " << int(it-myvector.begin()) << '\n';  else    std::cout << "match not found\n";}
Run:






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

// 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: [email protected]

Yu gdut

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






STL algorithm search, search_n (52)

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.