C++ 迭代器的next和prev
迭代器的下一个或上一个分别用next()和prev()获取。
#include <string>
#include <iostream>
#include <iterator>
#include <map>
using namespace std;
int main()
{
map<int, string> map1 = {{ 7,"a1" }, { 8, "a2" }, {9,"a3" }};
for (auto it = map1.begin(); it != map1.end(); it++)
{
if (prev(it) == map1.begin()) cout << "第二个元素 " << map1[it->first] << endl; // a2
if (next(it) == map1.end()) cout << "最后一个元素 " << map1[it->first] << endl; // a3
}
}