c ++对象的地图顺序

时间:2016-01-28 16:26:21

标签: c++ dictionary stl

我想知道为什么我的输出看起来像这样:

This : 1
a : 4
is : 2
just : 3
test : 5

当我的代码看起来像这样:

map<string, int> wordCount;
wordCount["This"] = 1;
wordCount["is"] = 2;
wordCount["just"] = 3;
wordCount["a"] = 4;
wordCount["test"] = 5;
for (map<string, int>::iterator it = wordCount.begin();
        it != wordCount.end(); it++)  {
    cout << it->first << " : " << it->second << endl;
}

我的问题是,地图是否以随机顺序存储对象?

1 个答案:

答案 0 :(得分:5)

地图按排序顺序存储内容。 body{ height: 100%; width: 100%; } .row { width: 100%; margin: 0px auto; max-width: 100%; height: 100%; } .column, .columns { position: relative; padding-left: 0em; padding-right: 0em; float: left; height: 100%; width: 100%; } #wrapper { width: 100%; height: 100%; right: 0%; top:0%; position: relative; } #parent { width: 100%; height: 100%; background: #00f; position: relative; border-radius: 0px; border: solid 0px white; right: 0px; } #div1 { width: 3224px; height: 2007px; left: 0; overflow: hidden; position: absolute; top: 0; background-image: url("https://xphub-resources.s3.amazonaws.com/customer/7dd00ec1-187f-42c2-859c-918d671a2895/img/NH_cidade.jpg"); background-size: cover; background-position: 0 0; background-repeat: no-repeat; right: 0%; } .wrapper-general { height: 100%; left: 0; overflow: hidden; position: absolute; top: 0; width: 100%; } #right { position: absolute; right: 0px; top: 50%; z-index: 99999; margin: 0px; width: 90px; height: 50px; } #left { position: absolute; left: 0px; top: 50%; z-index: 99999; margin: 0px; width: 90px; height: 50px; } #top { position: absolute; left: 50%; top: 0%; z-index: 99999; margin: 0px; width: 90px; height: 50px; } #bottom { position: absolute; left: 50%; bottom: 0%; z-index: 99999; margin: 0px; width: 90px; height: 50px; } 之前"This"的原因是"a"'T'之前出现在大多数(如果不是所有字符集)中'a'因此'T' < 'a'来自"This"之前,因为不考虑字符串的长度。

如果您将"a"更改为This,那么您将获得

this

Live Example