How to sort strings in a map

Source: Internet
Author: User

 

As we all know, map is designed based on a balanced binary tree. Therefore, whether it is directly written

For example

Map <string, string> str_map;

Str_map ["Alex"] = "Yes? ";

Str_map ["Alex"] = "No! ";

Then, the order in the map is

Alex-no!

Alex-yes?

Of course, this was what I realized.

The specific problem is that, I want to simplify it. Because you need to write a value in Buf, and then store it in Map <string, string>.

Sprintf (BUF, "instance-% d is OK", I + 1 );

ID _ = Buf;

Sprintf (BUF, "instance-% d is prepared", I + 1 );

If (str_map.find (ID _) = str_map.end () // If ID _ does not exist, add it

Str_map [ID _] = boost: lexical_cast <string> (BUF );

Then, the map will be written to the HTML file.

Result When instance> 9, the problem occurs.

Instance-1 is prepared

Instance-10 is prepared

Instance-11 is prepared

Instance-12 is prepared

Instance-2 is prepared

Instance-3 is prepared

Instance-4 is prepared

Instance-5 is prepared

Instance-6 is prepared

Instance-7 is prepared

Instance-8 is prepared

Instance-9 is prepared

If you have a better solution, please leave a message and share your thoughts.

My solution: how to sort strings in Map

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.