1. 程式人生 > >C++ 在容器A中查找容器B中的元素,並返回iterator(find_end)

C++ 在容器A中查找容器B中的元素,並返回iterator(find_end)

++ com == end efault red != color position

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

C++ 在容器A中查找容器B中的元素,並返回iterator(find_end)