# Basic example of MAP and set in STL

Source: Internet
Author: User

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!

Related Keywords:

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.