相信學(xué)習(xí)C++的人有很多人用過CString.find()函數(shù),,但是你有么有用過 std::find() 函數(shù)呢,? find函數(shù)主要實現(xiàn)的是在容器內(nèi)查找指定的元素,并且這個元素必須是基本數(shù)據(jù)類型的,。 查找成功返回一個指向指定元素的迭代器,,查找失敗返回end迭代器。 例一,,在數(shù)組中查找: # include <iostream>
# include <vector>
# include <algorithm> //注意要包含該頭文件
using namespace std;
int main()
{
int nums[] = { 3 , 1 , 4 , 1 , 5 , 9 };
int num_to_find = 5 ;
int start = 0 ;
int end = 5 ;
int * result = find( nums + start, nums + end, num_to_find );
if ( result == nums + end )
{
cout<< "Did not find any number matching " << num_to_find << endl;
}
else
{
cout<< "Found a matching number: " << *result << endl;
//the index of value 25 is 5
}
return 0 ;
}
例二,,在容器中查找: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
vector< int > v;
int num_to_find=25; //要查找的元素,類型要與vector<>類型一致
for ( int i=0;i<10;i++)
v.push_back(i*i);
vector< int >::iterator iter=std::find(v.begin(),v.end(),num_to_find); //返回的是一個迭代器指針
if (iter==v.end())
cout<< "ERROR!" <<endl;
else //注意迭代器指針輸出元素的方式和distance用法
cout<< "the index of value " <<(*iter)<< " is " << std::distance(v.begin(), iter)<<std::endl;
return 0;
}
另外還有一個函數(shù)find_if find_if函數(shù) 帶條件的查找元素,容器元素類型是類的時候,,不能使用find函數(shù),,只能通過find_if函數(shù)來實現(xiàn)。find_if函數(shù)依次的遍歷容器的元素,,返回第一個使函數(shù)為true的元素的迭代器,,如果查找失敗則返回end迭代器 #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template < typename T>
bool equal_3(T value){
return value==3;
}
int main(){
vector< int > vec;
vec.push_back(7);
vec.push_back(3);
vec.push_back(8);
vector< int >::iterator finda=find_if(vec.begin(),vec.end(),equal_3< int >);
if (finda!=vec.end())
cout<< "YES" <<*finda<<endl;
else
cout<< "ERROR" <<endl;
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
struct Point
{
int x;
int y;
};
struct PointFindByCoord : public std::binary_function<Point, Point, bool >
{
bool operator () ( const Point &obj1, const Point &obj2) const
{
return obj1.x == obj2.x && obj1.y == obj2.y;
}
};
int main()
{
std::vector<Point> v;
for ( int i = 0; i < 5; ++i)
{
for ( int j = 0; j < 5; ++j)
{
Point pt;
pt.x = i;
pt.y = j;
v.push_back(pt);
}
}
Point needFind;
needFind.x = 4;
needFind.y = 3;
std::vector<Point>::iterator iter=std::find_if(v.begin(),v.end(),std::bind2nd(PointFindByCoord(), needFind));
if (iter == v.end())
{
// 未找到
}
else
std::cout << "the index of value Point(" << (*iter).x << ", " << (*iter).y
<< ") is " << std::distance(v.begin(), iter) << std::endl;
return 0;
}
|
|