【发布时间】:2019-07-31 07:18:36
【问题描述】:
我读过重复但真的没有帮助我。
我正在尝试实现下一个行为。
有一个由对组成的向量{Thing, set < Thing >}
我想要{Thing, newSetOfThing < Thing >}的最终结果
其中'newSetOfThing'是与其他应用的差异
设置在向量中,但他自己。差异意味着具有所有值但包含在其他集合中。我正在使用std::set_difference。
用数字举一个更接近的例子。
vector = {[1, {3,4,5,7}], [2,{1,3,9}], [3, {1,2,12}]};
==>
vectorResult = {[1, {4,5,7}], [2, {9}], [3, {2,12} }
那么我的代码是这样的:
class Thing {
public:
Thing () {
};
~Thing () {};
int position; //id
bool operator<(const Thing &Other) const;
};
bool Thing::operator<(const Thing &Thing) const
{
return(Other.position<position);
}
//The original vector
vector<pair<Thing, set<Thing>>> pairWithSet;
// I fill vector here [...]
// variable I define to store partial results
set<Thing> differenceResult;
// Vector I want to fill for results
vector<pair<Thing, set<Thing>>> newPairWithSet;
// Operation
for (pair<Thing, set<Thing>> currentPair : pairWithSet){
Thing currentThing= currentPair.first;
differenceResult = currentPair.second;
for (int i=0; i<pairWithSet.size();i++) {
if (pairWithSet[i].first.position != currentThing.position) {
set_difference(differenceResult.begin(),
differenceResult.end(),
pairWithSet[i].second.begin(),
pairWithSet[i].second.end(),
differenceResult.begin());
}
newPairWithSet.push_back(pair<Thing, set<Thing>>(currentThing, differenceResult));
}
我向您解释了我的目标,但最后我认为问题更多地与我使用 set_difference 操作的错误程度以及我无法直接分配“事物”有关。所以 set_difference 没有办法检查它们是否相同。因为错误是
二进制'=':没有找到接受左操作数类型的运算符 'const Thing'(或没有可接受的转换
我说是因为可能还有其他错误可以达到行为,因为在我解决操作员问题之前我仍然无法调试。
我的问题是我是否需要声明 '=' 操作以及如何声明。或者,如果我错过了什么,我需要以另一种方式表演。
我可以确保问题是当我使用 set_difference 时,如果我评论这部分编译器会完成任务:
set_difference(differenceResult.begin(),
differenceResult.end(),
pairWithSet[i].second.begin(),
pairWithSet[i].second.end(),
differenceResult.begin());
我认为是因为最后它试图执行对 const 的赋值(因为std::pair 声明?),因为它说错误(那么显然编译器不知道如何操作)。所以我还不清楚如何执行递归 set_difference。
【问题讨论】:
-
在哪里你得到错误?您能否复制粘贴 full 和 complete 错误输出,包括任何可能的信息性消息。
-
编译时出错。错误 C2678 二进制“=”:未找到采用“const Thing”类型左侧操作数的运算符(或没有可接受的转换)|算法:4873
-
@FooBarExtension 请在此处按要求提供minimal reproducible example。
-
您确定要复制事物和集合吗?否则,您应该使用参考:
for(std::pair<...>&: pairWithSet)或更简单的auto&。
标签: c++ vector set std-pair set-difference