C++ 在容器A中查找最后出现的容器B中的元素,并返回iterator(find_end)

 

#include <iostream>     // cout
#include <algorithm>    // find_end
#include <vector>       // vector
using namespace std; 
bool myfunction (int i, int j) {
  return (i==j);
}

int main () {
  int myints[] = {1,2,3,4,5,1,2,3,4,5};
  vector<int> haystack (myints,myints+10);

  int needle1[] = {1,2,3};

  // using default comparison:
  vector<int>::iterator it;
  it = find_end (haystack.begin(), haystack.end(), needle1, needle1+3);

  if (it!=haystack.end())
    cout << "needle1 last found at position " << (it-haystack.begin()) << '\n';

  int needle2[] = {4,5,1};

  // using predicate comparison:
  it = find_end (haystack.begin(), haystack.end(), needle2, needle2+3, myfunction);

  if (it!=haystack.end())
    cout << "needle2 last found at position " << (it-haystack.begin()) << '\n';return 0;
}

 

posted @ 2018-10-20 23:55  anobscureretreat  阅读(485)  评论(0编辑  收藏  举报