[英]Returning an intersection set of two vectors from a function
有人可以幫我嗎,為什么以后我在一個單獨的函數中打印出結果向量out_v時,我似乎無法得到交集? 我不知道這是什么問題,我正在使用指向數組*運算符+(Array * inp_v)下面的函數的指針傳遞向量,后者應稍后從set_intersection返回指向所得向量的指針。 可能是我的set_intersection無法正常工作還是在其他方面?
我已將所有stl標頭包含在內。
Array* operator+ (Array* inp_v)
{
XorArray *v1 = (XorArray *)inp_v;
XorArray *out_v = new XorArray();
vector<double>::iterator iVect1 = v1->vect.begin();
vector<double>::iterator iVect2 = this->vect.begin();
vector<double> Result;
sort(iVect1, v1->vect.end());
sort(iVect2, this->vect.end());
vector<double>::iterator newEnd = set_intersection(iVect1, v1>vect.end(), iVect2, this->vect.end(),Result.begin());
while (newEnd != Result.end()
{
out_v->vect.push_back(*newEnd);
++newEnd;
}
return out_v;
}
一個主要問題是result
向量為空 ,這意味着result.begin() == result.end()
。 要在result
向量中追加新項目,您需要使用例如std::back_inserter
。
我建議您檢查一個std::set_intersection
參考。
使用back_inserter將內容推送到結果容器中。 例如,請參閱http://en.cppreference.com/w/cpp/algorithm/set_intersection
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.