比较两个不同列表的内容
Comparing the contents of two different lists
我正在尝试比较两个不同列表的内容。我正在使用迭代器循环浏览列表。我正在检查列表1中的最后一个元素是否显示在列表2中。这是代码的摘要
/* 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
。这是一个棘手的部分,我正在比较两个不同的容器之间。实际上,两个不同的数据架构。值得庆幸的是,thoguh 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}')
我目前对 *迭代器 *操作员的理解是,它将像使用 *操作员使用指针一样放置迭代器吗?
这是不正确的吗?
因为 back
返回参考,而不是迭代器(注意错误内容: (operand type is 'blablabla {aka edgeLineShape}')
)。您可以像普通一样比较它:
if (temp == pathContour.back())
但实际上,temp
并不需要,因为您只是在这里使用它,因此您可以做
if (*lineIterator == pathContour.back())
另外,如果您使用的是C 11或更高版本,则应查看auto
,这可以转动:
for(plf::colony<edgeLineShape>::iterator lineIterator = p_lineList->begin(); lineIterator != p_lineList->end(); lineIterator++)
进入这个:
for (auto lineIterator = p_lineList->begin(); lineIterator != p_lineList->end(); lineIterator++)
或foreach,可以将其浓缩到更简洁的地方:
for (auto lineIterator : p_lineList)
正如Semyon所提到的,它是一个参考,因此您不能简单地使用DereFence Operator。
但是,您可以这样做:
*(pathContour.end()--)
如果您使用迭代器。
问题中的代码没有比较容器。也许这是一个XY问题?
等同于您的代码的C IC是:
#include <algorithm>
auto it = std::find(p_lineList->begin(), p_lineList->end(), , pathCountour.back());
if (it != p_lineList->end()) { /* element found */ }
,但您可能应该考虑一些可以避免线性搜索的容器。
相关文章:
- 测试两个类型列表中的所有组合
- 创建一个棋盘格或"Interweave"两个链接列表。IE 更改两个链表的指针
- C++声明双链表,使用两个 for 循环双向遍历列表并打印
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 我如何创建一个列表,然后从中创建两个列表,其中一个用于偶数,另一个用于奇数?
- C++如果两个模板函数都与参数列表匹配,将调用哪个模板
- 如何在两个列表中比较和获取非包含值
- 如何在构造器的成员初始值设定项列表中调用两个函数?
- 将两个列表合并为一个蛇形列表
- C++ 分段错误:11 错误,同时编码将两个数字相加的链接列表
- 使用特征查找两个列表之间的差异
- 如何在这两个分类的链接列表之间获得开关的位置
- 如何合并两个双重链接列表(访问下一个链接)
- 通过单词列表将两个字符串移动以比较每个单词
- 添加一个节点,并在通用树中的两个给定节点之间找到路径成本,其中c 中的儿童列表
- 如何比较C 列表中的两个连续元素
- 两个排序的链接列表C 的交点
- 如何在两个包含指针的地图中创建列表
- 合并两个单链接列表以形成第三个列表的功能
- 两个单链列表共享同一个节点和析构函数两次删除相同的内存