Detailed map, Multimap, Unordered_map, unordered_multimap

Source: Internet
Author: User

Detailed map, Multimap, Unordered_map, unordered_multimap

I believe that a lot of students and I just contact C + + STL, is deeply attracted by it. But trying to understand each template class is not an easy task. We should be on the vector, list, stack, queue and other classes to understand, so today detailed introduction of the next few very powerful but a little less understood class map, Multimap, Unordered_map, Unordered_multimap. At first glance, it's pretty much a map, but it's definitely different. The following is an explanation of the use of these four classes, as well as the differences.

MAP1) Start with a simple example

2) #include <map>

3) #include <iostream>

4) using namespace Std;

5)

6) int main ()

7) {

8) map<char,int> MSI;

9)//map::operator[]

Ten) msi[' x '] = 3;

one) msi[' z '] = 3;

Msi[' g '] = 2;

msi[' y '] = 1;

)//map::at ()

() Auto Beg = Msi.begin ();

for (; Beg!=msi.end (); beg++) {

cout<<msi.at (Beg->first) << "";

18)}

cout<<endl;)

) return 1;

21)}

Results:

2 3 1 3

Code Analysis:

L map::operator[]

mapped_type& operator[] (const key_type& k);
mapped_type& operator[] (key_type&& k);

The function uses K,v as a KV pair by calling the Mak_pair function, inserting v into the map by using the Insert function as K.

L Map::at

mapped_type& at (const key_type& k);
Const mapped_type& at (const key_type& k) const;
The function uses K to find the V value.
l See from the result that the print is printed in increments of K value to print the V value.
2) Continue from Example 1)

Let's change the code in Example 1.

1) #include <map>

2) #include <iostream>

3) using namespace Std;

4)

5) int main ()

6) {

7) map<char,int> MSI;

8)//map::operator[]

9) msi[' x '] = 3;

Ten) msi[' x '] = 4; This changes the

One) msi[' g '] = 2;

msi[' y '] = 1;

//map::at ()

) Auto Beg = Msi.begin ();

for (; Beg!=msi.end (); beg++) {

cout<<msi.at (Beg->first) << "";

17)}

) cout<<endl;

) return 1;

20)}

Results:

2 4 1

Code Analysis:

L There is a duplicate key value x, then X/3 is not the right to be overwritten???? So just print out 2 41!!!!!

3) Continue from Example 2)

1) int main ()

2) {

3) map<char,int> MSI;

4)//map::operator[]

5) msi[' x '] = 3;

6) msi[' x '] = 4;

7) msi[' g '] = 2;

8) msi[' y '] = 1;

9)//map::at ()

) Auto Beg = Msi.begin ();

one) for (; Beg!=msi.end (); beg++) {

cout<<msi.at (Beg->first) << "";

13)}

cout<<endl;)

15)

() Auto get = Msi.equal_range (' x ');

(+) Auto Skbeg = Get.first;

) Auto Skend = Get.second;

for (; skbeg!=skend; skbeg++) {

cout<<skbeg->first<< ":" <<skbeg->second<< "";

21)}

cout<<endl;)

23)

) return 1;

25)}

Results:

2 4 1

X:4

Code Analysis:

L Map::equal_range

Pair<const_iterator,const_iterator> Equal_range (const key_type& k) const;
Pair<iterator,iterator> Equal_range (const key_type& k);

According to reference, this function should return the upper and lower bounds of element containing K, but we did not print out, X:3 x:4. So, uh, we came to the conclusion that in map, K and V are one-to-one relationships, and there is no case where a k corresponds to multiple v values.

4) OK to continue

1) int main ()

2) {

3) map<char,int> MSI;

4)//map::operator[]

5) msi[' C '] = 3;

6) msi[' h '] = 5;

7) msi[' B '] = 2;

8) msi[' a '] = 1;

9)

)//insert G

One) Msi.insert (pair<char,int> (' G ', 4));

//copy Map

Map<char,int> Newmap;

Newmap.insert (Msi.begin (), Msi.find (' G '));

//map::at ()

() Auto Beg = Msi.begin ();

(; Beg!=msi.end (); beg++) {

cout<<beg->first<< "," <<msi.at (Beg->first) << "";

19)}

cout<<endl;)

21)

Auto Newbeg = Newmap.begin ();

(; Newbeg!=newmap.end (); newbeg++)

cout<<newbeg->first<< "," <<newmap.at (Newbeg->first) << "";

cout<<endl;)

/*auto get = Msi.equal_range (' x ');

) Auto Skbeg = Get.first;

Auto Skend = Get.second;

for (; skbeg!=skend; skbeg++) {

cout<<skbeg->first<< ":" <<skbeg->second<< "";

31)}*/

62L

) cout<<endl;

34)

return 1;

36)}

Results:

A->1 b->2 c->3 g->4 h->5

A->1 b->2 c->3

Code Analysis:

L Map::insert

Template <class p> pair<iterator,bool> Insert (p&& val);
Template <class inputiterator>
void Insert (Inputiterator first, Inputiterator last);

The two insert functions are used here, the first of which we deposit a pair object as an element in the map, and the second we deposit the element in the MSI from start to G in Newmap.

4) TBC

There are some member functions not introduced, I would like to introduce in a few other classes, in fact, are very simple ...

MULTIMAP1) straight to the subject

1) #include <map>

2) #include <iostream>

3) #include <string>

4) using namespace Std;

5)

6) BOOL CMP (const string &a, const string &b)

7) {

8) return A.compare (b) >0?true:false;

9)}

10)

one) int main ()

12) {

BOOL (*CMP_PT) (const string&, const string&) = CMP;

multimap<string, int, BOOL (*) (const string&, const string&) > MCI (CMP_PT); Descending

Mci.insert (pair<string,int> ("BB", 2));

Mci.insert (pair<string,int> ("AA", 1));

) Mci.insert (pair<string,int> ("CC", 3));

Mci.insert (pair<string,int> ("GG", 6));

Mci.insert (pair<string,int> ("CC", 5));//Repeat

20)

) Auto Beg = Mci.begin ();

for (; Beg!=mci.end (); beg++) {

cout<<beg->first<< "<<beg->second<<" ";

24)}

cout<<endl;)

() Auto Skget = Mci.equal_range ("cc");

) Auto Skbeg = Skget.first;

Auto Skend = Skget.second;

for (; skbeg!=skend;skbeg++)

cout<<skbeg->first<< "<<skbeg->second<<" ";

cout<<endl;)

32)}

Results:

Gg->6 cc->3 cc->5 bb->2 aa->1

Cc->3 cc->5

Code Analysis:

L Descending Output

Here are two methods, one is a function pointer and the other is a class. The former is used here. The class method is simpler and only needs to be overloaded with operator (), as shown below.

struct classcomp {

  bool operator() (constchar& lhs, const char& rhs)const

  {return lhs<rhs;}

};

std::multimap<char,int,classcomp> fourth;

L Initialize Data

Data cannot be given by operator[], without this member function.

L've come to the point.

It can be found that despite the addition of duplicate key value CC/5, but we still print out the duplicate key value pairs, so we can say that Multimap and map one of the biggest difference is that Multimap can achieve a pair of multiple storage mode!!!!!!!!

UNORDERED_MULTIMAP1) still straight to the subject

1) #include <iostream>

2) #include <unordered_map>

3) using namespace Std;

4)

5) int main ()

6) {

7) unordered_multimap<char,int> umci,insertum={{' B ', 12},{' W ', 11}};

8) Umci.insert (Make_pair (' d ', 5));

9) Umci.insert (pair<char,int> (' A ', 1));

Umci.insert (Make_pair (' B ', 2));

One) Umci.insert (Make_pair (' G ', 8));

Umci.insert (Insertum.begin (), Insertum.end ());

13)

) Auto Beg = Umci.begin ();

for (; Beg!=umci.end (); beg++)

cout<<beg->first<< "<<beg->second<<" ";

cout<<endl;)

18)

) return 1;

20)}

Results:

G->8 b->12 b->2 w->11 a->1 d->5

Code Analysis:

L Unordered Storage

It is obvious that Unordered_multimap's disordered storage features, which is the biggest difference from Multimap.


Detailed map, Multimap, unordered_map, unordered_multimap

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.