stl(一)------如何遍历map中的元素,以及如何在map中插入元素
#include "stdafx.h"
#include <map>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
map<int, char> maptest;
maptest.insert(make_pair(1,'a'));
maptest.insert(make_pair(2,'a'));
maptest.insert(make_pair(3,'a')); //插入元素
map<int, char>::iterator iter; //利用遍历器 获取存储的在map中的元素
iter = maptest.begin();
for (iter; iter != maptest.end(); ++iter)
{
cout<<"first的值是:\t"<<iter->first<<endl;
cout<<"second的值是:\t"<<iter->second<<endl;
}
return 0;
}
输出结果:
first的值是: 1
second的值是: a
first的值是: 2
second的值是: a
first的值是: 3
second的值是: a
还没有评论,来说两句吧...