find_if function "reprint" in STL

Source: Internet
Author: User

As mentioned in the previous article, the Find () function can only handle simple types of content, that is, Default Type, you will get an error if you want to use a custom type of data as the basis for finding it! Another function is described here find_if ()Usage of

This is a more powerful version of Find (). This example shows Find_if (), which takes the parameters of a function object as a parameter and uses it to make more complex evaluation objects and to pay for the given lookup criteria.
Let's say we have some chronological records with events and dates in our list. We want to find out what happened in 1997.

The code is as follows:

[C-sharp]View Plaincopy
  1. //----------------------------------------------------------------------------------------
  2. Desc:stl_find_if () _how to find things on an STL list MkII
  3. Author:pigfly
  4. data:2010.12.01
  5. Copyright (C) pigfly
  6. //----------------------------------------------------------------------------------------
  7. #include <iostream>
  8. #include <string>
  9. #include <list>
  10. #include <algorithm>
  11. Using namespace std;
  12. Class EventIsIn1997 {
  13. Public
  14. bool operator () (string& eventrecord) {
  15. //year field was at position-4 characters in Eventrecord
  16. return Eventrecord.substr (11,4) = ="1997";
  17. //return this->substr (11,4) = = "1997"
  18. }
  19. };
  20. int main (void) {
  21. list<string> Events;
  22. //String positions 0123456789012345678901234567890123456789012345
  23. Events.push_back ("January 1995 Draft plan of House prepared");
  24. Events.push_back ("February 1996 detailed plan of House prepared");
  25. Events.push_back ("Ten January 1997 Client agrees to Job");
  26. Events.push_back ("January 1997 Builder starts work on bedroom");
  27. Events.push_back ("April 1997 Builder finishes work");
  28. list<String>::iterator eventiterator = find_if (Events.begin (), Events.end (), EventIsIn1997 ());
  29. //Find_if completes the first time EventIsIn1997 () () returns True
  30. //For any object. It returns an iterator to that object which we
  31. //Can dereference to get the object, or if EventIsIn1997 () () Never
  32. //returned true, FIND_IF returns end ()
  33. if (Eventiterator==events.end ()) {
  34. cout << "Event not found in list" << Endl;
  35. }
  36. else {
  37. cout << *eventiterator << Endl;
  38. }
  39. }

Output:

Ten January 1997 Client agrees to job

Note here that the third parameter of Find_if () is EventIsIn1997 (), which is an imitation function that receives a string object that defines the search criteria I want in the interior of the operator (), in this case, the lookup condition is: eventrecord.substr (11,4 = = "1997", note that the return type of the functor here must be of type bool, this objective reaction in the find_if () function lookup process is matched!

Let's look at the lookup process for a custom structure of a data type:

Code:

[C-sharp]View Plaincopy
  1. //----------------------------------------------------------------------------------------
  2. Desc:stl_find_if () used in vector container, struct data
  3. Author:pigfly
  4. data:2010.12.01
  5. Copyright (C) pigfly
  6. //----------------------------------------------------------------------------------------
  7. #include <iostream>
  8. #include <vector>
  9. #include <string>
  10. #include <algorithm>
  11. Using namespace std;
  12. struct value_t
  13. {
  14. int A;
  15. int b;
  16. };
  17. Class Vector_finder
  18. {
  19. Public
  20. Vector_finder ( const int A, const int b): M_v_a (a), M_v_b (b) {}
  21. bool operator () (vector<struct value_t>::value_type &value)
  22. {
  23. return (value.a==m_v_a) && (value.b = M_v_b);
  24. }
  25. Private
  26. int m_v_a;
  27. int m_v_b;
  28. };
  29. int main ()
  30. {
  31. Vector<value_t> My_vector;
  32. value_t My_value;
  33. My_value.a = 11; my_value.b = 1001;
  34. My_vector.push_back (My_value);
  35. My_value.a = 12; my_value.b = 1002;
  36. My_vector.push_back (My_value);
  37. My_value.a = 13; my_value.b = 1003;
  38. My_vector.push_back (My_value);
  39. My_value.a = 14; my_value.b = 1004;
  40. My_vector.push_back (My_value);
  41. Vector<value_t>::iterator it = find_if (My_vector.begin (), My_vector.end (), Vector_finder (13,1003));
  42. if (it = = My_vector.end ())
  43. cout<<"Not found!"  <<endl;
  44. Else
  45. cout<<"found value A:" << (*it). A <<", B:" << (*it) .b<<endl;
  46. return 0;
  47. }

Output:

Found value a:13, b:1003

Here we also construct a functor, the class Vector_finder, the Vector_finder () function, noting the relationship between its structure and the structure we are looking for, and we find that they are very similar.

The focus here is on the construction of class Vector_finder!

Now look at the application in the map container:

Code

[C-sharp]View Plaincopy
  1. //----------------------------------------------------------------------------------------
  2. Desc:stl_find_if () used in map container, string data
  3. Author:pigfly
  4. data:2010.12.01
  5. Copyright (C) pigfly
  6. //----------------------------------------------------------------------------------------
  7. #include <iostream>
  8. #include <map>
  9. #include <string>
  10. #include <algorithm>
  11. Using namespace std;
  12. Class Map_finder
  13. {
  14. Public
  15. Map_finder ( string cmp_string): M_string (cmp_string) {}
  16. bool operator () (const map<int,string>::value_type pair)
  17. {
  18. return Pair.second = = m_string;
  19. }
  20. Private
  21. string m_string;
  22. };
  23. int main ()
  24. {
  25. map<int,string> my_map;
  26. My_map.insert (Make_pair (Ten,"China"));
  27. My_map.insert (Make_pair ("USA"));
  28. My_map.insert (Make_pair ("中文版"));
  29. My_map.insert (Make_pair (+,"Hongkong"));
  30. map<int,string>::iterator it = find_if (My_map.begin (), My_map.end (), Map_finder ("中文版"));
  31. if (it = = My_map.end ())
  32. cout<<"Not found!"  <<endl;
  33. Else
  34. cout<<"found key:" << (*it) .first<<", Value:" << (*it) .second<<endl;
  35. return 0;
  36. }

Output:

Found Key:30,vlaue:english

Because here is just pay attention to the use of find_if (), do not care about its details, if you want to learn more, such as the template prototype of the function, and even the entire STL, you can see the "STL Source Code Analysis"

Find_if () in the real application, the query object can be originally empty vector, in the platform software has been implemented.

find_if function "reprint" in STL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.