这是我的代码:
#include <iostream>
#include <map>
using namespace std;
int main() {
map<int , int > myMap;
map<int , int>::iterator it;
myMap.insert(pair<int , int>(1,2));
myMap.insert(pair<int , int>(671,223));
myMap.insert(pair<int , int>(353,245352));
it = myMap.end() - 1;
cout << it->first << it->second << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译此代码会产生以下编译错误:
error: no match for ‘operator-’ (operand types are ‘std::map<int, int>::iterator {aka std::_Rb_tree_iterator<std::pair<const int, int> >}’ and ‘int’)
it = myMap.end() - 1;
Run Code Online (Sandbox Code Playgroud)
我不知道为什么我会收到此错误,因为我认为所有类型的迭代器都允许算术运算.
Sto*_*ica 40
并非所有迭代器类别都支持算术运算,这是一种误解.如果您的目标是编写更多通用代码,则可以使用std::prev:
it = std::prev(myMap.end());
Run Code Online (Sandbox Code Playgroud)
它期望一个双向迭代器,它std::map是迭代器.如果要将迭代器移动多个步骤,它还可以接受第二个参数,该参数指定移动迭代器的距离.
另外,当你传递一个随机访问迭代器时,它将与算术一样快.
| 归档时间: |
|
| 查看次数: |
2793 次 |
| 最近记录: |