【发布时间】:2016-09-01 14:07:38
【问题描述】:
在 C++ 标准地图库中,对迭代函数的调用会返回一个迭代器指针,
std::map<int, int> map;
std::map<int, int>::iterator ite = map.find(1);
但是,当使用迭代器时,必须作为指针变量访问,
int first = ite->first;
int second = ite->second;
当我们将从find 函数获得的值放入非指针时,为什么会出现这种情况。正确的语法不应该是:
std::map<int, int>::iterator *pIte = map.find(1)
或者如果使用原始语法,
int first = ite.first;
int second = ite.second;
因为我们获取的是一个值,而不是函数返回值的指针? documentation 中也没有说明。为什么会这样?
【问题讨论】:
-
迭代器 act 作为指针,在某些情况下是指针。您建议的语法是指向迭代器的指针。
-
与
shared和unique_ptr相同,它们是应该像指针一样工作的对象,所以这是有意的。