C++进阶:STL算法7--查找

1. 简介

函数 作用 文档
find(beg,end,val) 利用==操作符,对[beg,end)的元素与val进行比较。当匹配时结束搜索,返回该元素的InputIterator find()
find_if(beg,end,pred) 使用函数pred代替==操作符执行find() find_if()
find_first_of(beg1,end1,beg2,end2) 在[beg1,end1)范围内查找[beg2,end2)中任意一个元素的第一次出现。返回该元素的Iterator find_first_of()
find_first_of(beg1,end1,beg2,end2,pred) 使用函数pred代替==操作符执行find_first_of()。返回该元素的Iterator find_first_of()
find_end(beg1,end1,beg2,end2) 在[beg1,end1)范围内查找[beg2,end2)最后一次出现。找到则返回最后一对的第一个ForwardIterator,否则返回end1 find_end()
find_end(beg1,end1,beg2,end2,pred) 使用函数pred代替==操作符执行find_end()。返回该元素的Iterator find_end()
adjacent_find(beg,end) 对[beg,end)的元素,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的ForwardIterator。否则返回end adjacent_find()
adjacent_find(beg,end,pred) 使用函数pred代替==操作符执行adjacent_find() adjacent_find()

2. 示例代码

  • find
// find example
#include      // std::cout
#include     // std::find
#include        // std::vector

int main () {
  // using std::find with array and pointer:
  int myints[] = { 10, 20, 30, 40 };
  int * p;

  p = std::find (myints, myints+4, 30);
  if (p != myints+4)
    std::cout << "Element found in myints: " << *p << '\n';
  else
    std::cout << "Element not found in myints\n";

  // using std::find with vector and iterator:
  std::vector myvector (myints,myints+4);
  std::vector::iterator it;

  it = find (myvector.begin(), myvector.end(), 30);
  if (it != myvector.end())
    std::cout << "Element found in myvector: " << *it << '\n';
  else
    std::cout << "Element not found in myvector\n";

  return 0;
}
  • find_if
// find_if example
#include      // std::cout
#include     // std::find_if
#include        // std::vector

bool IsOdd (int i) {
  return ((i%2)==1);
}

int main () {
  std::vector myvector;

  myvector.push_back(10);
  myvector.push_back(25);
  myvector.push_back(40);
  myvector.push_back(55);

  std::vector::iterator it = std::find_if (myvector.begin(), myvector.end(), IsOdd);
  std::cout << "The first odd value is " << *it << '\n';

  return 0;
}
  • find_first_of
// find_first_of example
#include      // std::cout
#include     // std::find_first_of
#include        // std::vector
#include        // std::tolower

bool comp_case_insensitive (char c1, char c2) {
  return (std::tolower(c1)==std::tolower(c2));
}

int main () {
  int mychars[] = {'a','b','c','A','B','C'};
  std::vector haystack (mychars,mychars+6);
  std::vector::iterator it;

  int needle[] = {'A','B','C'};

  // using default comparison:
  it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3);

  if (it!=haystack.end())
    std::cout << "The first match is: " << *it << '\n';

  // using predicate comparison:
  it = find_first_of (haystack.begin(), haystack.end(),
                      needle, needle+3, comp_case_insensitive);

  if (it!=haystack.end())
    std::cout << "The first match is: " << *it << '\n';

  return 0;
}
  • find_end
// find_end example
#include      // std::cout
#include     // std::find_end
#include        // std::vector

bool myfunction (int i, int j) {
  return (i==j);
}

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

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

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

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

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

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

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

  return 0;
}
  • adjacent_find
// adjacent_find example
#include      // std::cout
#include     // std::adjacent_find
#include        // std::vector

bool myfunction (int i, int j) {
  return (i==j);
}

int main () {
  int myints[] = {5,20,5,30,30,20,10,10,20};
  std::vector myvector (myints,myints+8);
  std::vector::iterator it;

  // using default comparison:
  it = std::adjacent_find (myvector.begin(), myvector.end());

  if (it!=myvector.end())
    std::cout << "the first pair of repeated elements are: " << *it << '\n';

  //using predicate comparison:
  it = std::adjacent_find (++it, myvector.end(), myfunction);

  if (it!=myvector.end())
    std::cout << "the second pair of repeated elements are: " << *it << '\n';

  return 0;
}

3. 练习

你可能感兴趣的:(C++进阶:STL算法7--查找)