【发布时间】:2015-09-27 20:10:22
【问题描述】:
我创建一个地图只是为了学习目的来存储一些键值对。如果我使用begin() 函数打印地图的第二个字段,我可以打印地图的第二个字段,但是当我尝试使用end() 对地图的最后一个元素执行相同操作时,它无法打印第二个字段。以下是我的代码:
#include <iostream>
#include <cstdlib>
#include <map>
#include <string>
#include <stdio.h>
using namespace std;
map<int,std::string> arr;
map<int,std::string>::iterator p;
int main(int argc, char** argv) {
arr[1] = "Hello";
arr[2] = "Hi";
arr[3] = "how";
arr[4] = "are";
arr[5] = "you";
p = arr.begin();
printf("%s\n",p->second.c_str());
p = arr.end();
printf("%s\n",p->second.c_str());
return 0;
}
【问题讨论】:
-
对于 STL 容器,".end()" 产生一个迭代器到 "1-past-the-end-element" 另见:stackoverflow.com/questions/15252002/…
标签: c++ dictionary std stdmap c++-standard-library