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
- //----------------------------------------------------------------------------------------
- Desc:stl_find_if () _how to find things on an STL list MkII
- Author:pigfly
- data:2010.12.01
- Copyright (C) pigfly
- //----------------------------------------------------------------------------------------
- #include <iostream>
- #include <string>
- #include <list>
- #include <algorithm>
- Using namespace std;
- Class EventIsIn1997 {
- Public
- bool operator () (string& eventrecord) {
- //year field was at position-4 characters in Eventrecord
- return Eventrecord.substr (11,4) = ="1997";
- //return this->substr (11,4) = = "1997"
- }
- };
- int main (void) {
- list<string> Events;
- //String positions 0123456789012345678901234567890123456789012345
- Events.push_back ("January 1995 Draft plan of House prepared");
- Events.push_back ("February 1996 detailed plan of House prepared");
- Events.push_back ("Ten January 1997 Client agrees to Job");
- Events.push_back ("January 1997 Builder starts work on bedroom");
- Events.push_back ("April 1997 Builder finishes work");
- list<String>::iterator eventiterator = find_if (Events.begin (), Events.end (), EventIsIn1997 ());
- //Find_if completes the first time EventIsIn1997 () () returns True
- //For any object. It returns an iterator to that object which we
- //Can dereference to get the object, or if EventIsIn1997 () () Never
- //returned true, FIND_IF returns end ()
- if (Eventiterator==events.end ()) {
- cout << "Event not found in list" << Endl;
- }
- else {
- cout << *eventiterator << Endl;
- }
- }
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
- //----------------------------------------------------------------------------------------
- Desc:stl_find_if () used in vector container, struct data
- Author:pigfly
- data:2010.12.01
- Copyright (C) pigfly
- //----------------------------------------------------------------------------------------
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- Using namespace std;
- struct value_t
- {
- int A;
- int b;
- };
- Class Vector_finder
- {
- Public
- Vector_finder ( const int A, const int b): M_v_a (a), M_v_b (b) {}
- bool operator () (vector<struct value_t>::value_type &value)
- {
- return (value.a==m_v_a) && (value.b = M_v_b);
- }
- Private
- int m_v_a;
- int m_v_b;
- };
- int main ()
- {
- Vector<value_t> My_vector;
- value_t My_value;
- My_value.a = 11; my_value.b = 1001;
- My_vector.push_back (My_value);
- My_value.a = 12; my_value.b = 1002;
- My_vector.push_back (My_value);
- My_value.a = 13; my_value.b = 1003;
- My_vector.push_back (My_value);
- My_value.a = 14; my_value.b = 1004;
- My_vector.push_back (My_value);
- Vector<value_t>::iterator it = find_if (My_vector.begin (), My_vector.end (), Vector_finder (13,1003));
- if (it = = My_vector.end ())
- cout<<"Not found!" <<endl;
- Else
- cout<<"found value A:" << (*it). A <<", B:" << (*it) .b<<endl;
- return 0;
- }
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
- //----------------------------------------------------------------------------------------
- Desc:stl_find_if () used in map container, string data
- Author:pigfly
- data:2010.12.01
- Copyright (C) pigfly
- //----------------------------------------------------------------------------------------
- #include <iostream>
- #include <map>
- #include <string>
- #include <algorithm>
- Using namespace std;
- Class Map_finder
- {
- Public
- Map_finder ( string cmp_string): M_string (cmp_string) {}
- bool operator () (const map<int,string>::value_type pair)
- {
- return Pair.second = = m_string;
- }
- Private
- string m_string;
- };
- int main ()
- {
- map<int,string> my_map;
- My_map.insert (Make_pair (Ten,"China"));
- My_map.insert (Make_pair ("USA"));
- My_map.insert (Make_pair ("中文版"));
- My_map.insert (Make_pair (+,"Hongkong"));
- map<int,string>::iterator it = find_if (My_map.begin (), My_map.end (), Map_finder ("中文版"));
- if (it = = My_map.end ())
- cout<<"Not found!" <<endl;
- Else
- cout<<"found key:" << (*it) .first<<", Value:" << (*it) .second<<endl;
- return 0;
- }
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