首页 > 编程语言 > 详细

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

时间:2018-10-21 00:32:06      阅读:239      评论:0      收藏:0      [点我收藏+]

 

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

原文:https://www.cnblogs.com/sea-stream/p/9823550.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!