0
map<int, string>::reverse_iterator& it = temp.rbegin();
it
- >指向垃圾键值地图C++逆向迭代
it++
- >指向正确的键值
map<int, string>::iterator& it = temp.begin();
it
- >点从开始正确的密钥值。
请协助。
map<int, string>::reverse_iterator& it = temp.rbegin();
it
- >指向垃圾键值地图C++逆向迭代
it++
- >指向正确的键值
map<int, string>::iterator& it = temp.begin();
it
- >点从开始正确的密钥值。
请协助。
您的陈述不正确。如果temp
不为空,那么*temp.rbegin()
确实是地图中的最后一个值,并且*temp.begin()
是第一个值。
(然而,底层迭代反向的开始是普通的结束迭代器 - 但你看不到,除非你对反向迭代器调用base()
)
您必须有一个错误的代码填充地图。你可以通过测试一个简单的例子来验证这个例子,比如
#include <algorithm>
#include <map>
#include <iostream>
using namespace std;
int main()
{
map<int, char> coll;
// insert elements from 1 to 9
for (int i=1; i<=9; ++i) {
coll[i] = static_cast<char>(i+'a'-1); // because adding 96 is less obvious that we're indexing based at letter a
}
// print all element in reverse order
for_each (coll.rbegin(), coll.rend(),
[](pair<int, char> mapinfo) { cout << mapinfo.second << " "; });
cout << endl;
}
Kerrek SB已经给出了正确的答案,但是你发布的代码不应该编译:你不能用临时值初始化非const引用。 –