The common search algorithms are as follows:
Find ()
Find_if ()
//
Search_n ()
Search ()
Find_end ()
Find_first_of ()
Adjacent_find ()
//
These two methods are common, try out all the containers, but the lookup efficiency is slow, is the linear lookup
Find () This complexity is linear complexity
FIND_IF () This complexity is linear complexity
Attention:
1, if it is an ordered interval, you can use the ordered interval lookup algorithm (Binary_search includes () Lower_bound () Upper_bound ()).
2, the associative container (set Multiset map Multimap) has the equivalent member function find (), this find () complexity is logarithmic complexity, fast
3,,string has the equivalent member function find ()
//
#include <iostream> #include <algorithm> #include <list>using namespace Std;int main () {List<int > ilist;for (int i = 1; I <= 8; i++) {Ilist.insert (Ilist.end (), i);} for (int i = 1; I <= 8; i++) {Ilist.insert (Ilist.end (), i);} for (List<int>::iterator iter = Ilist.begin (); ITER! = Ilist.end (); iter++) {cout << *iter << ';} cout << Endl;list<int>::iterator pos1;pos1 = Find (Ilist.begin (), Ilist.end (), 4); list<int>:: Iterator pos2;if (pos1! = Ilist.end ()) {Pos2 = Find (++POS1, Ilist.end (), 4);} Elsecout << "not found 4! "<< endl;if (pos1! = Ilist.end () && Pos2! = Ilist.end ()) {--pos1;++pos2;for (List<int>::iterator iter = POS1; Iter! = Pos2; iter++) cout << *iter;} cout << endl;//system ("pause"); return 0;}
#include <iostream> #include <algorithm> #include <vector>//#include <functional>using namespace Std;int main () {vector<int> ivec;vector<int>::iterator pos;for (int i = 1; I <= 9; i++) Ivec.push_b ACK (i); for (Vector<int>::iterator iter = Ivec.begin (); ITER! = Ivec.end (); iter++) cout << *iter << '; c Out << Endl;pos = find_if (Ivec.begin (), Ivec.end (), bind2nd (Greater<int> (), 3));//Function object is > 3cout << * POS << endl;//modulus modulo Operation pos = find_if (Ivec.begin (), Ivec.end (), Not1 (bind2nd (Modulus<int> (), 3))); cout << *pos << endl;//system ("pause"); return 0;}
//
Pre-defined Function objects:
Negate<type> () equal_to<type> () plus<type> () not_equal_to<type> () Minus<type > () less<type> ()
Multiplies<type> () greater<type> () divides<type> () less_equal<type> () modulus<type> ( )
Greater_equal<type> () logical_not<type> () logical_and<type> () logical_or<type> ()
Pre-defined function adapters
Bindlst (OP, value)
bind2nd (OP, value)
Not1 (OP)
Not2 (OP)
Mem_fun (OP)
Ptr_fun (OP)
Sequential interval Lookup algorithm
Binary_search ()
Includes ()
Lower_bound ()
Upper_bound ()
#include <iostream> #include <set>//using namespace Std;int main () {//Set auto-sort, is automatically balanced by advanced red-black tree set<int> Iset;iset.insert (Iset.insert); Iset.insert ( -1); Iset.insert (124); for (set<int>::iterator iter = Iset.begin (); Iter! = Iset.end (); iter++) cout << *iter << "cout << endl;set<int>::iterator pos;pos = iset.find;//Find is Set member function if (pos! = Iset.end ()) {cout << "found! "<< *pos << Endl;} Else{cout << "not found! "<< Endl;} System ("pause"); return 0;}
#include <iostream> #include <string>//using namespace Std;int main () {string S ("AnnaBelle"); string::size_ Type pos = S.find ("Belle");p OS = S.find ("Bell"); if (pos! = string::npos) cout << "Found! "<< Pos << endl;elsecout <<" not found! "<< Endl;//system (" pause "); return 0;}
STL STL Algorithm for C + +-Find algorithm (1)