STL entry guide
Involved Source: http://download.csdn.net/detail/nuptboyzhb/4239649
STL has the following six major parts:
LIterators)
The iterator can be understood as a template pointer. The iterator technology allows the program to repeatedly access the content of STL containers;
LAlgorithm (alogrithms)
STL provides many data structure algorithms that are defined in the STD namespace and can be used through # include <algorithm>. Common algorithms include for_each (), find (), find_if (), count (), replace (), copy (), sort (), and merge.
LContainer (containers)
Common Containers include vector containers vector, list containers of two-way linked list, deque containers of double-end queues, set containers, multi-set containers Multiset, map containers, and multimap containers.
LFunction object)
LMemory distributor (allocators)
LAdapter)
An adapter is a generalized template type. The adapters provided by STL are mainly divided into iterator adapter, function Object Adapter, and container adapter, which are used for conversion of the iterator, function object, and container respectively.
1. namespace
Use the standard STD namespace, using namespace STD;
2. Some suggestions
1. In debug mode, ignore the following warning message: # pragma warning (Disable: 4786)
2. When the following container is defined:
Do not write it as vector <list <int> veclis, but vector <list <int> veclis, that is, add a space between '>' and '>;
3.
Clever Use of the copy Algorithm
Output of the following program;
#include <string>#include <set>#include <iostream>using namespace std;#pragma warning(disable: 4786)int main(int argc, char* argv[]){ set <string> strset; set <string>::iterator si; strset.insert("cantaloupes"); strset.insert("apple"); strset.insert("orange"); strset.insert("banana"); strset.insert("grapes"); strset.insert("grapes"); // This one overwrites the previous occurrence for (si=strset.begin(); si!=strset.end(); si++) { cout << *si << " "; } cout << endl; return 0;}
We can use the following statement to replace the output of the For Loop:
Copy (strset. Begin (), strset. End (), ostream_iterator <string> (cout ,""));
4. How to map containers for some class applications
Map is a key used to obtain the value. When you use your own defined class instead of some data types (such as INT), you must ensure that the custom class contains the following functions and operations;
1. default constructor (usually empty)
2. copy constructors
3. Overload operator '='
The preceding three points are required. You can also add other functions and operators;
Example:
# Include <string> # include <iostream> # include <vector> # include <map> using namespace STD; Class cstudent {public: int nstudentid; int Nage; public: // define an empty constructor cstudent () {}// full constructor cstudent (INT nsid, int Na) {nstudentid = nsid; Nage = Na ;} // define a copy constructor cstudent (const cstudent & ob) {nstudentid = ob. nstudentid; Nage = ob. nage;} // overload operator = void operator = (const cstudent & ob) {nstudentid = ob. nstudentid; Nage = ob. nage ;}}; int main (INT argc, char * argv []) {Map <string, cstudent> mapstudent; mapstudent ["Joe Lennon"] = cstudent (103547, 22); mapstudent ["Phil McCartney"] = cstudent (100723, 22); mapstudent ["Raoul Starr"] = cstudent (107350, 24 ); mapstudent ["Gordon Hamilton"] = cstudent (102330, 22 ); // access via the name cout <"the student number for Joe Lennon is" <(mapstudent ["Joe Lennon"]. nstudentid) <Endl; return 0 ;}
5.
Typedef Application
For example:
typedef set <int> SET_INT;
Typedef set_int: iterator set_int_iter
6. Notes for the iterator
Iterator-For any container other than the vector, you can only step one at a time in a forward ction through the container. that is you can only use the ++ operator, not the -- or + = Operator on it.
For vector only you can use any of + =, --,-=, ++, and all the comparison operators <, <=, >=, == ,! =. For most containers, only the following operators can be applied:++, <,<=, >,>=, == ,! =;For vector containers, you can also use+ =,
--,-=, ++Operator.
Reverse_iterator-If you want to step backwards instead of forwards through a non-vector container, replace iterator with reverse_iterator,Begin ()With
Rbegin (), AndEnd ()WithRend (),
++Will then traverse backwards.
Const_iterator-A forward iterator that returnsConst value. Use this if you want to make it clear that this points to a read-only value.
Const_reverse_iterator-A reverse iterator that returnsConst value.
7. Algorithms
The functions applied to the template when the algorithm is used. You can easily sort, search, operate, and exchange elements in the template container.
The container itself is not passed to the algorithm, but the container'sIteratorTo the algorithm. Therefore, the data type restricted by the algorithm is the data type of the iterator.
Example:
# Include <algorithm> // if you want to use an // algorithm this is the header used. # include <numeric> // (for accumulate) # include <vector> # include <iostream> using namespace STD; int testscore [] = {67, 56, 24, 78, 99, 87, 56}; // predicate that evaluates a passed test limitation function bool passed_test (int n) {return (n> = 60 );} // predicate that evaluates a failed testbool failed_test (int n) {return (n <60);} int Ma In (INT argc, char * argv []) {int total; // use an array to initialize the vector container vector <int> vectestscore (testscore, testscore + sizeof (testscore) /sizeof (INT); // generate the iterator VI vector <int>: iterator VI; // sort and output the value of sort (vectestscore. begin (), vectestscore. end (); cout <"sorted test scores:" <Endl; For (Vi = vectestscore. begin (); vi! = Vectestscore. end (); vi ++) {cout <* VI <"," ;}cout <Endl; // min_element returns an iterator Vi = min_element (vectestscore. begin (), vectestscore. end (); cout <"the lowest score was" <* VI <". "<Endl; // same with max_element Vi = max_element (vectestscore. begin (), vectestscore. end (); cout <"the highest score was" <* VI <". "<Endl; // use a predicate function to determine the number who passed // use a qualified function to specify the type of input value; here we limit the score cout <count_if (vectestscore. begin (), vectestscore. end (), passed_test) <"out of" <vectestscore. size () <"students passed the test" <Endl; // and who failed cout <count_if (vectestscore. begin (), vectestscore. end (), failed_test) <"out of" <vectestscore. size () <"students failed the test" <Endl; // sum the scores Total = accumulate (vectestscore. begin (), vectestscore. end (), 0); // then display the average cout <"average score was" <(total/(INT) (vectestscore. size () <Endl; return 0 ;}
8. template derivation and embedding
1. As a member variable of the class; for example:
class CParam{ string name; string unit; vector <double> vecData;};2. inherit the Template
class CParam : public vector <double>{string name;string unit;};9. templates in the template
To create a more complex data structure, you can create a template in a template. We recommend that you first define the template with the typedef keyword.
Example:
#include <iostream>#include <vector>using namespace std;typedef vector <int> VEC_INT;int inp[2][2] = {{1, 1}, {2, 0}}; // Regular 2x2 array to place into the templateint main(int argc, char* argv[]){ int i, j; vector <VEC_INT> vecvec; // if you want to do this in all one step it looks like this // vector <vector <int> > vecvec; // Fill it in with the array VEC_INT v0(inp[0], inp[0]+2); // passing two pointers // for the range of values to be copied to the vector VEC_INT v1(inp[1], inp[1]+2); vecvec.push_back(v0); vecvec.push_back(v1); for (i=0; i<2; i++) { for (j=0; j<2; j++) { cout << vecvec[i][j] << " "; } cout << endl; } return 0;}// Output:// 1 1// 2 0