1.源码实现
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
map<int, string> mapStudent;
map<int, string>::iterator it;
/*插入数据*/
mapStudent.insert(pair<int, string>(1, "student one"));
mapStudent.insert(pair<int, string>(2, "student two"));
mapStudent.insert(pair<int, string>(3, "student three"));
cout << "2: " << mapStudent[2] << endl;
/*遍历数据*/
for(it=mapStudent.begin(); it!=mapStudent.end(); it++)
{
cout << it->first << ": " << it->second << endl;
}
/*清空数据*/
mapStudent.clear();
/*遍历数据*/
for(it=mapStudent.begin(); it!=mapStudent.end(); it++)
{
cout << it->first << ": " << it->second << endl;
}
return 0;
}
2.编译源码
$ g++ -o example example.cpp
3.运行及其结果
$ ./example
2: student two
1: student one
2: student two
3: student three
网友评论