stl(一)------如何遍历map中的元素,以及如何在map中插入元素

分手后的思念是犯贱 2022-10-06 08:00 334阅读 0赞
  1. #include "stdafx.h"
  2. #include <map>
  3. #include <iostream>
  4. using namespace std;
  5. int main(int argc, char* argv[])
  6. {
  7. map<int, char> maptest;
  8. maptest.insert(make_pair(1,'a'));
  9. maptest.insert(make_pair(2,'a'));
  10. maptest.insert(make_pair(3,'a')); //插入元素
  11. map<int, char>::iterator iter; //利用遍历器 获取存储的在map中的元素
  12. iter = maptest.begin();
  13. for (iter; iter != maptest.end(); ++iter)
  14. {
  15. cout<<"first的值是:\t"<<iter->first<<endl;
  16. cout<<"second的值是:\t"<<iter->second<<endl;
  17. }
  18. return 0;
  19. }

输出结果:

first的值是: 1
second的值是: a
first的值是: 2
second的值是: a
first的值是: 3
second的值是: a

发表评论

表情:
评论列表 (有 0 条评论,334人围观)

还没有评论,来说两句吧...

相关阅读