如何正确使用stl::map::iterator
的指针运算?我可以在STL :: map :: iterator上做指针运算
在下面的示例中,我创建了stl::map
和stl::map::iterator
并尝试通过增加迭代器来访问迭代器第二个键。
#include <iostream>
#include <map>
using namespace std;
int main() {
map <string, string> peoples;
peoples["paul"] = "friend";
peoples["galactus"] = "foe";
map<string, string>::iterator my_iter = ++peoples.begin();
cout << my_iter->first << endl;
return 0;
}
当使用++peoples.begin();
编译是OK,但如果使用1 + peoples.begin();
,出现错误。
问题站:如何使用指针算术stl::map::iterator
?
是否有任何容器有类似的困难,虽然我知道用stl::vector::iterator
你可以使用指针算术?
那么cpp如何将stl :: map :: iterator的++转换为++? ,它是最后添加的数字,不是吗? –
@Café吸血鬼什么号码加到底是什么? – user2079303
不,这是误会。我问的是'++'是如何最终(在编译过程结束时)在cpp中翻译的。我所建议的是,'++'必须产生一个增加迭代器值的数字。 –