看个代码:
// map默认会按照.first的字母顺序排列
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<string, string> map1;
map<string, string>::iterator mapit;
map<string, string>::iterator saveit;
map1["fab"] = "2";
map1["dja"] = "3";
map1["dfe"] = "4";
map1["tw"] = "5";
cout << "Map size: " << map1.size() << endl;
mapit = map1.begin();
while (mapit != map1.end())
{
cout << "Element key: " << mapit->first << ", value: " << mapit->second << endl;
mapit++;
}
return 0;
}
运行结果:
Map size: 4
Element key: dfe, value: 4
Element key: dja, value: 3
Element key: fab, value: 2
Element key: tw, value: 5
Press any key to continue
看到了吗,map中的first成员被按字母顺序给输出来了!