find函式(C++ 標準模板庫函式)

本詞條是多義詞,共2個義項
更多義項 ▼ 收起列表 ▲

find函式提供了一種對數組、STL容器進行查找的方法。

基本介紹

  • 中文名:find
  • 語言:C++
  • 頭檔案:#include <algorithm>
函式功能,函式原型,輸入參數,返回值,實例,

函式功能

查找一定範圍內元素的個數。
查找[first,last)範圍內,與toval等價的第一個元素,返回一個疊代器。如果沒有這個元素,將返回last。
(Returns an iterator to the first element in the range [first,last) that compares equal toval. If no such element is found, the function returnslast.)

函式原型

template <class InputIterator, class T>   InputIterator    find (           InputIterator first,            InputIterator last,            const T& val   );

輸入參數

first, last
輸入查詢序列的開始和結束位置,注意:這兩個參數為疊代器。
val
要查詢的值

返回值

返回查詢結果的疊代器

實例

#include <iostream>#include <vector>#include <algorithm>using namespace std;int main(){  //為vecIntegers添加數據  vector<int> vecIntegers;  for (int nNum=0; nNum<10;++nNum)  {      vecIntegers.push_back(nNum);  }  //列印數據  vector<int>::const_iterator iElementLocator;  for (iElementLocator=vecIntegers.begin();       iElementLocator != vecIntegers.end();           ++iElementLocator)  {           cout << *iElementLocator << ' ';  }  cout << endl;    /*****************關鍵代碼******************************/  //查找數字 3  //注意:返回值為疊代器  vector<int>::iterator iElementFound;  iElementFound = find(vecIntegers.begin(), vecIntegers.end(), 3);   if (iElementFound != vecIntegers.end()) //如果找到結果  {    cout << *iElementFound << endl;   }  else  {      cout << "沒有找到結果!" << endl;  }  /****************************************************/  return 0;}
運行結果
0 1 2 3 4 5 6 7 8 93Press any key to continue

相關詞條

熱門詞條

聯絡我們