關於map中含數字string的排序問題

來源:互聯網
上載者:User

 

    大家都知道map內部是按照平衡二叉樹來設計的,所以無論是直接寫

如

map<string, string> str_map;

str_map["alex"] = "yes?";

str_map["Alex"] = "no!";

之後,這個map裡的順序就是

Alex-no!

alex-yes?

當然,這是我之後才頓悟到的. 

具體問題是這樣,我簡化一下,由於需要在buf裡寫入值,再存到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() )   //若ID_不存在,則添加進去

     str_map[ID_] = boost::lexical_cast<string> (buf);

之後,這個map會被寫到html檔案裡。

結果當Instance > 9時,問題出現了。

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

如果有更好的解決方式請留言,共同交流呀

我的解決方式:關於map中含數字string的排序方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.