- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有两个集合,我正在尝试做一个并集(我在做一个交集时得到同样的错误)。这是错误:
error C3892: 'std::_Tree_const_iterator<_Mytree>::operator *' : you cannot assign to a variable that is const
代码片段(如果我用 --> 注释掉该行,那么代码会编译并且我围绕 union 的方式工作正常):
set<Line *>::iterator it;
set<Line *> * newSet = new set<Line *>();
leftLines = pLeft->getSet();
rightLines = pRight->getSet();
-->it = set_union(leftLines->begin(),leftLines->end(),rightLines->begin(), rightLines->end(), newSet->begin());
for(it = leftLines->begin(); it != leftLines->end(); it++)
{
newSet->insert(*it);
}
for(it = rightLines->begin(); it != rightLines->end(); it++)
{
newSet->insert(*it);
}
it = newSet->begin();
while(it != newSet->end())
{
result->insert(*it);
it++;
}
我确定这很愚蠢,但我有点迷路了。我认为代码片段应该足够了,但我可以提供任何其他需要的东西。谢谢。
最佳答案
这是 C++,而不是 Java [编辑:或 .NET]。您几乎肯定想要替换(例如):
set<Line *> * newSet = new set<Line *>();
只有:
set<Line *> newSet;
...或者,更好的是,可能只是:
set<Line> newSet;
尽管根据您发布的代码无法确定,但您的 left
和 right
很有可能不应该处理指针要么——如果他们打算做任何类似的事情,引用可能更有意义(尽管,正如我所说,仅根据您发布的内容,不可能肯定地说)。
完成后,您会遇到一个小问题:set
(或multiset
、map
)上的“普通”迭代器> 或 multimap
) 实际上是一个 const_iterator。一旦将某些东西插入到关联容器中,就不允许更改它,因为这可能会破坏集合的不变性(正在排序)。如果要更改现有项目,则需要从容器中删除 if,进行更改,然后将更改的对象插入回容器中。在您的情况下,您只是插入新项目,因此您需要一个 insert_iterator
。
由于您不打算修改 left
或 right
,因此您也可以将它们视为 const
:
std::set_union(left.cbegin(), left.cend(),
right.cbegin(), right.cend(),
std::inserter(newSet, newSet.end()));
如果您决定自己模拟 set_union,您可以这样做:
std::set<Line> newSet(left.cbegin(), left.cend());
std::copy(right.cbegin(), right.cend(), std::inserter(newSet, newSet.end()));
编辑:
您通常不想传递指向容器的指针,而是传递迭代器到容器中。例如,要打印出内容,您显然现在有类似的东西:
void print_data(std::vector<Line *> const *data) {
for (int i=0; i<data->size(); i++)
std::cout << *(*data)[i] << "\n";
}
它可能有更多的格式等等,但目前我们将忽略这些细节并假设它就是这么简单。要直接从您选择的容器写入数据,您通常需要一个接受任意类型迭代器的模板:
template <class inIt>
void print_data(inIt begin, inIt end) {
while (begin != end)
std::cout << *begin++ << '\n';
}
但是,我们可以更进一步,将输出也指定为迭代器:
template <class inIt, class outIt>
void print_data(inIt begin, inIt end, outIt dest) {
while (begin != end) {
*dest++ = *begin++;
*dest++ = '\n';
}
}
您可以再走一步,允许用户指定要在项目之间使用的分隔符,而不是总是使用“\n”,但在这一点上,您只是在复制已经在项目中的内容标准库 -- std::copy
和 std::ostream_iterator
的组合,这就是您可能希望在现实中处理此问题的方式:
std::copy(newSet.begin(), newSet.end(),
std::ostream_iterator<Line>(std::cout, "\n"));
但是请注意,就标准库而言,ostream_iterator
只是另一个迭代器。如果你只是想打印出 left
和 right
的并集,你甚至可以跳过创建一个集合来保存那个并集,直接打印出来:
std::set_union(left.cbegin(), left.cend(),
right.cbegin(), right.cend(),
std::ostream_iterator<Line>(std::cout, "\n"));
ostream_iterator
写入文件而不是将内容放入普通集合这一事实与标准库完全无关。它有几个迭代器类,可以将输出写入任何模拟正确类的迭代器。
现在,我可能是仓促行事,可以这么说——在将数据写入控制台之前,可能需要对数据进行其他处理。我的意思并不是说您必须将 union 直接写入标准输出,而只是说在打印出来之前您不一定必须将它写入其他集合。
关于c++ - 使用 set_union 和 set_intersection 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5665058/
我认为此处描述的 set_intersection STL 函数:http://www.cplusplus.com/reference/algorithm/set_intersection/在数学意义
使用 中的功能时, 通常有一个额外的参数来自定义比较。但是我不太明白关于参数的描述(Documentation of set_intersection)。 Binary function that
有什么方法可以对两种不同类型的集合执行 std::set_intersection 吗? 我有两套: std::set l_set1; std::set l_set2; 我可以为它们定义一些比较器来检
这个问题在这里已经有了答案: std::back_inserter for a std::set? (2 个答案) 关闭 2 年前。 是否可以将 std::set_intersection 或其他
我正在编写一个代码,根据对象的 ID 将两个对象 vector 相交。我收到运行时错误。我试图找出问题所在,但不知道为什么?你能帮我吗? #include #include #include u
struct Cord { int x_cord; int y_cord; Cord(int x = 0,int y = 0):x_cord(x
我想找到两张 map 之间的交点。我的 map 有结构 map , 其中line是一个结构。问题是当我使用 set_intersection为了执行交集,我得到了图像中表示的以下错误。 下面是我的代码
我需要获取两个 vector 之间的交集列表。在我的例子中, vector 是用户类型的 vector 。所以为了获得封装的数字,我必须使用比较器函数。 我还希望能够获得与偏移量的交集。例如,给定两个
我正在阅读有关 set_intersection 的内容,它似乎期望用户提前分配正确数量的空间(或更多),但这不是很奇怪吗?在 C++ 中,您经常使用 std::vector 动态分配空间。为什么 s
std::set m{1,2, 4}; std::set n{2,3, 4}; std::set mn; std::set::iterator it; it=set_intersection(m.be
This question建议使用 std::set_intersection 来查找两个数组的交集。使用 std::find 不是同样有效吗? int a[5] = {1, 2, 3, 4, 5};
我有一个类有两个属性: set ens1_; set ens2_; 现在,我有一个方法可以找到这两个集合之间的交集。这是我在我的方法中写的: set ens; set::iterator it;
我想知道如何 thrust::set_intersection有效,但从我的测试结果来看,我对这个函数的作用更加困惑。 举几个例子: const int size1 = 5; const int si
我有一个简单的要求,我需要从另一个 vector 中的字符串主列表中找到一个 vector 中字符串的出现。一开始我可以很容易地做到这一点: vector custom_list; set maste
我在尝试理解使用自定义比较器时 std::set_intersection 的语法。我正在尝试获取集合 one 和 two 的交集,这将导致集合 (intersect) 仅包含元素 f2. 我得到错误
我知道这些命令适用于两组。 是否有任何简单快速的方法可以为超过两组的情况执行此操作。 我想我可以为此使用某种循环,但也许有更好的方法。 谢谢 最佳答案 对于集合并集,如果要查看 M 个集合中哪个集合的
大家好:)我想创建一个个人集合类并重载运算符/=,就我的类而言,该运算符应该用于获取两个集合的插值。我收到以下错误: 错误:分配只读位置 '__result.std::_Rb_tree_const_i
下面代码的复杂度是多少? set S1, S2, ans; set_intersection(S1.begin(), S1.end(), S2.begin(), S2.end(), inserter(
我想知道标准库中是否有任何工具可以同时计算两个排序范围之间的交集和差集。带有以下签名的东西: template Output3 decompose_sets (Input1 first1, Inpu
std::set_intersection允许我通过将元素输出到 输出迭代器 来检索两个 std::set 实例之间的所有共同元素。在我的特定情况下,我只对检查两个集合是否有任何共同元素感兴趣。 我目
我是一名优秀的程序员,十分优秀!