This article mainly uses STL Sino-German map and set containers, uses some of their own functional functions (including iterators), and introduces their basic usage methods, is a familiar process.
Basic use of MAP:
# Include "stdafx. H "# include <iostream> # include <set> # include <string> # include <vector> # include <map> using namespace STD; int main () {// define the map object Map <string, float> mymap; mymap ["Jack"] = 98.5; mymap ["Bomi"] = 98.0; mymap ["Kate"] = 97.6; Map <string, float >:: iterator ITM; For (ITM = mymap. begin (); ITM! = Mymap. end (); ITM ++) {// output cout based on the key value and mapped data <(* ITM ). first <":" <(* ITM ). second <Endl;} int K = 0; CIN> K; return 0 ;}
Basic use example of set:
# Include "stdafx. H "# include <iostream> # include <set> # include <string> # include <vector> using namespace STD; int main () {set <int> myset; myset. insert (8); myset. insert (1); myset. insert (12); myset. insert (6); myset. insert (8); // here, Because 8 has already been inserted before, duplicate elements are not inserted. Set <int>: iterator its; // set container iterator cout <"Forward traversal:" <""; for (its = myset. begin (); its! = Myset. end (); its ++) // forward traversal {cout <* its <"" ;}cout <Endl <"reverse traversal:" <""; set <int>: reverse_iterator rit; // set the reverse iterator for (RIT = myset. rbegin (); rit! = Myset. rend (); rit ++) {cout <* rit <"" ;}// Delete the element myset whose key value is 6. erase (6); cout <Endl <"reverse traversal after deletion:" <""; for (RIT = myset. rbegin (); rit! = Myset. rend (); rit ++) {cout <* rit <"" ;}// retrieve myset for elements in set. insert (17); myset. insert (10); cout <Endl; its = myset. find (10); // use the iterator for search. If it is not found, end () is returned (). if (its! = Myset. end () cout <"found" <* its <Endl; else cout <"no query element found" <Endl; its = myset. find (100); If (its! = Myset. end () cout <"found" <* its <Endl; else cout <"no query element found" <Endl; int K = 0; cin> K; return 0 ;}
For details, see the comments in the source code. Thank you!