Saturday, July 18, 2015

The ascend order of the first item in MAP class: some comments

The map class can automatically sort the first item in ascending order. For example, when first value is numbers:

map<intint> test;
       test[2] = 100;
       test[1] = 200;
       test[3] = 300;

In test, the value will be sorted like: 
(1,200)
(2,100)
(3,300)

What if the first value is not numbers? It still arranged in ascending order in terms of its referred value, e.g., the characters will be sorted according to its ASK codes:

map<charint> months;
       months['a'] = 1;
       months['e'] = 2;
       months['b'] = 3;

In months, the value will be sorted like: 
(a,1)
(b,3)
(e,2)

For the other kinds of value, we can think the same way as above. 

Since the first value is sorted in ascending order, what if some first value is the same? 
like: 

map<intint> test;
       test[2] = 100;
       test[1] = 200;
       test[1] = 300;

In test, the value will be sorted like: 
(1,300)
(2,100)

In this case, the previous value '1' will be overwrite by the latter '1'. As a result, there are only two, in stead of three items in it. 
Why?  ?  As the second value can be accessed as test[2], and you get its value 100. If there are two items which have the same first value, we can not differentiate them, like test[100] should be 200 or 300? Therefore, we cannot use this character to sort and array with the same items!!



No comments:

Post a Comment