Td::find:
Find container elements, find can only look for container elements for < basic data type >
[CPP]View Plain Copy
- #include <iostream>
- #include <vector>
- #include <algorithm>
- int main ()
- {
- Std::vector<int> v;
- for (int i = 0; i <; ++i)
- V.push_back (i);
- Std::vector<int>::iterator iter = Std::find (V.begin (), V.end (), 3);
- if (iter = = V.end ())
- Std::cout << "Can not find value 3 in V" << Std::endl;
- Else
- Std::cout << "The index of Value" << (*iter) << "is" << std::d istance (V.begin (), ITER) << Std::endl;
- return 0;
- }
Std::find_if
Find a container element conditionally, the container type is < class >, you cannot find it using Find, so use find_if to find
[CPP]View Plain Copy
- #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 ())
- {
- Not found
- }
- Else
- Std::cout << "The index of Value point (" << (*iter). x << "," << (*iter). Y
- << "is" << std::d istance (V.begin (), ITER) << Std::endl;
- return 0;
- }
C + + STL Algorithm:std::find, std::find_if