473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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"

Please help me it is urgent

Any suggestions will be highly appreciated
Sep 25 '06 #1
1 2709
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

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: kittykat | last post by:
Hi, I was wondering if you could help me. I am writing a program in C++, and the problem is, i have very limited experience in this language. I would like my user to enter a specific pattern, and...
4
by: Code4u | last post by:
I need to write an algorithm that sheds the outliers in a large data set, for example, I might want to ignore the smallest 2% of values and find the next smallest. Boost has a nth_element...
1
by: aredo3604gif | last post by:
On Sun, 10 Apr 2005 19:46:32 GMT, aredo3604gif@yahoo.com wrote: >The user can dynamically enter and change the rule connection between >objects. The rule is a "<" and so given two objects: >a <...
6
by: Ravi | last post by:
Hi, I need to find the non-recursive algorithm to find the height of a Binary Tree. Regards, SunLight.
5
by: Mike Labosh | last post by:
In VB 6, the Form_QueryUnload event had an UnloadMode parameter that let me find out *why* a form is unloading, and then conditionally cancel the event. In VB.NET, the Closing event passes a...
5
by: Draw | last post by:
Hi All, Just a thought, about the find() algorithm in the C++ STL. I read that the find algorithm can take a range of iterators. If it does not find the element it is looking for in that range...
10
by: Christian Chrismann | last post by:
Hi, I've a question on the STL find algorithm: My code: int main( void ) { vector< ClassA* myVector; ClassA *ptrElement1 = ...;
10
by: vermarajeev | last post by:
Hi guys, I have problem regarding find algorithm used in STL Defination directly from book The find algorithm is an operation (function) that can be applied to many STL containers. It...
10
by: socondc22 | last post by:
my program is trying to use the babylonian algorithm in order to find the square root... i have a number for the number to have the square root taken of and also a number to run the loop... Whenever...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.