Tworzę mapę tylko do celów edukacyjnych do przechowywania par klucz-wartość. Jeśli wydrukuję drugie pole mapy przy użyciu funkcji begin()
, będę mógł wydrukować drugie pole mapy, ale gdy spróbuję zrobić to samo z ostatnim elementem mapy przy użyciu end()
, nie będzie można wydrukować drugiego pola. Poniżej jest mój kod:Nie można uzyskać drugiego pola mapy przy użyciu końca()
#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;
}
dla kontenerów STL, ".end()" daje iteratorowi element "1-past-the-end-element" zobacz także: http: // stackoverflow.com/questions/15252002/what-is-the-the-the-the-end-iterator-in-stl-c – user2950911