最近刚开始使用STL中的容器,发现map中的元素在insert之后,会自动依key排序。
#include <string>
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<int ,string> mapStudent;
mapStudent.insert (pair<int ,string>(3,"xiaoming"));
mapStudent.insert (pair<int ,string>(1,"hua"));
mapStudent.insert (pair<int ,string>(2,"liu"));
map<int ,string>::iterator it;
for(it=mapStudent.begin ();it!=mapStudent.end();it++)
{
int i=it->first;
string str=it->second;
cout << i << endl;
}
return 1;
}
上面输出的是:
1
2
3