471,831 Members | 1,097 Online

# find algorithm

180 100+
Hi, EveryBody

This question is really interesting one.

My question is related to "STL Find Algorithm"

Defination Direct from book
The find algorithm is an operation (function) that can be applied to many STL containers. It searches a subrange of the elements in a container (or all the elements), looking for an element that is "equal to" a specified value; the equality operator (==) must be defined for the type of the container's elements"
Now my questions are
1) How can I use "the equality operator (==)" in my class as defined above. PLease visualize with an example

2) Also my requirement is to only compare the pointers rather than subrange of the elements in a container (or all the elements). Is there anything else other than "find algorithm" defined in STL which I can use to compare pointers?

OR will I need to define my own function which behaves much like "find" but with only difference that "my function will only compare pointers in its logic"

Any suggestions will be highly appreciated
Sep 25 '06 #1
1 2599
rengaraj
168 100+
The find algorithm is an operation (function) that can be applied to many STL containers. It searches a subrange of the elements in a container (or all the elements), looking for an element that is "equal to" a specified value; the equality operator (==) must be defined for the type of the container's elements.

Expand|Select|Wrap|Line Numbers
1. #include <vector>  // vector class library
2. #include <list>       // list class library
3. #include <algorithm>       // STL algorithms class library
4. using namespace std;
5. ...
6. list<int> nums;
7. list<int>::iterator nums_iter;
8.
9. nums.push_back (3);
10. nums.push_back (7);
11. nums.push_front (10);
12.
13. nums_iter = find(nums.begin(), nums.end(), 3); // Search the list.
14. if (nums_iter != nums.end())
15. {
16.     cout << "Number " << (*nums_iter) << " found." << endl; // 3
17. }
18. else
19. {
20.     cout << "Number not found." << endl;
21. }
22.
23. // If we found the element, erase it from the list.
24. if (nums_iter != nums.end()) nums.erase(nums_iter);
25. // List now contains: 10 7
The find algorithm searches the specified subrange of the container's elements and stops when it finds the first element equal to the specified value, as defined by the equality (==) operator as applied to the container's elements. If this operator is defined for a programmer-defined type (as is the case with the string class), then a search for the programmer-defined type can be done just as easily as for a built-in type.

Note that the search value (the third argument to the find function) must be of the same type as the elements stored in the container, or at least of a type that the compiler can automatically convert. In many cases, it will be necessary to create and initialize a temporary data object for this purpose.

The return value of the find function is an iterator specifying the position of the first matching element. If no matching element is found, the return value is equal to the iterator specifying the end of the element subrange (in the example above, the end of the list).
Source: http://www.msoe.edu/eecs/ce/courseinfo/stl/find.htm
Feb 17 '07 #2