C++primer (Fourth edition) Review notes-second: containers and algorithms

Source: Internet
Author: User

Associative containers

1, the essential difference between the associated container and the sequential container is that the associated container is the key to store and read the elements and the key is stored in order, and the sequential container is the location of the container to access the elements, order of accession to the container of the successively related.
2, map storage Key-value pair

p(v1,v2);//构造给定值的pairmake_pair(v1,v2);//以函数make_piar构造pair

6, Map is a collection of key_value, can be understood as an associative array, with key as the subscript to access the element. Such as:

map<string,int>  word_cnt;//记录单词的出现次数,空word_cnt["C++Primer"]=2;//注意:以下标访问时,如果该key不存在,则会自动加入该key的一个元素,并将其value赋值为给定值。

Vactor does not have access to the non-existent elements.
7, the map iterator points to a key value pair in the container, where pair's key is a const object and can only modify its value.
8, mulitmap and mulitset delete function erase only with one parameter version, will delete all elements of the key in the container, and return the number of deletes, and the iterator will only delete its specified element.
9, Mulitmap and mulitset elements of the same key, stored in the adjacent location, the traversal can be guaranteed to return the same key elements. So count (key) returns the number of elements of the key, and find (key) returns the first function of the key.

Here is a simple example of applying a map container: Reading a file, recording the number of occurrences of each word in the file, and outputting the occurrences of the word and the number of occurrences in dictionary order
#include <iostream>#include <string>#include <map>#include <fstream>#include <sstream>using namespace STD;intMain () {Ifstream ifile;stringFileNamecout<<"input filename:"<<endl;//filename= "Ifile.txt";    Cin>>filename; Ifile.open (Filename.c_str ());if(!ifile) {cout<<"Open File failed!";return-1; } Map<string,int>word_cnt;stringStrstringWord while(Getline (IFILE,STR))//Read the file by line and save{IstringstreamISS (str); while(Iss>>word)//Will break down the words by word and record the number of occurrences++word_cnt[word]; } Map<string,int>:: Const_iterator mit=word_cnt.begin ();/* Output The number of occurrences of each word in dictionary order (that is, the map dictionary is sorted by key) */     for(; Mit!=word_cnt.end (); ++mit) {stringTemp"Time");if(mit->second>1) temp+="S";cout<<mit->first<<"occurs:"<<mit->second<<temp<<endl; }return 0;}

C++primer (Fourth edition) Review notes-second: containers and algorithms

Related Article

Contact Us

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.