【发布时间】:2018-03-08 08:08:50
【问题描述】:
我正在尝试比较两个不同列表的内容。我正在使用迭代器来遍历列表。我正在检查列表 1 中的最后一个元素是否出现在列表 2 中。这是代码的 sn-p
/* This section will loop through the list to make sure that the line segment that was added to the
* contour path is updated to be visited
*/
for(plf::colony<edgeLineShape>::iterator lineIterator = p_lineList->begin(); lineIterator != p_lineList->end(); lineIterator++)
{
edgeLineShape temp = *lineIterator;
if(temp == *(pathContour.back()))
{
lineSet = true;
lineIterator->setVisitedStatus(true);
break;
}
}
pathContour 定义为std::vector<edgeLineShape> pathContour。这是棘手的部分,我在两个不同的容器之间进行比较。实际上是两种不同的数据结构。值得庆幸的是 plf::colony 数据类型满足 C++ 容器的要求,但不满足。
当我去编译时,我在该行给出了一个错误:
if(temp == *(pathContour.back())
这是这一行的错误:
error: no match for 'operator*' (operand type is '__gnu_cxx::__alloc_traits<std::allocator<edgeLineShape> >::value_type {aka edgeLineShape}')
我目前对迭代器的 * 运算符的理解是,它将取消引用迭代器,就像使用 * 运算符取消引用指针一样?
这不正确吗?
【问题讨论】:
-
std::vector<T>::back()不返回迭代器。它返回对最后一个元素的引用,因此无需使用operator*取消引用它 -
另外:如果
pathContour为空,*(pathContour.back()会导致未定义的行为。
标签: c++ list iterator containers