[英]Need help can't figure out why my header file function declarations won't work
[英]STL iterator won't work? Can't figure out why
我有一個有一個私人成員的班級:
std::set<unsigned long> Sset;
我有這個功能的問題:
原型:
Set& Union (Set&, Set&);
碼:
Set& Set::Union (Set& s1, Set& s2)
{
set<unsigned long>::iterator a;
set<unsigned long>::iterator j;
for (a = s1.Sset.begin(); a!=s1.Sset.end(); ++a)
for (j = s2.Sset.begin(); j!=s2.Sset.end(); ++j)
if (*a = *j)
{
Sset.insert(*a);
break;
}
return *this;
}
我得到編譯器錯誤表達式必須是*a=*j
的可修改左值
使用迭代器j一切正常,但它不接受*a
有任何幫助或解釋嗎? 謝謝
你應該在if語句中使用==
嗎?
經典錯誤:-) - 比較需要兩個等號
if (*a == *j)
if (*a = *j)
是一項任務
if (*a == *j)
您可以一次將一組插入另一組:
std::set<int> s1, s2;
s1.insert(s2.begin(), s2.end());
集合成員是獨一無二的,所以沒有什么可擔心的。
您不能分配set::iterator
指向的值。
為什么?
因為集合保持其元素的任何順序 ( set
,特別是,已排序 )以允許快速檢索,因此您無法手動告訴它放置元素的位置。
而是insert
元素insert
正確的位置。
話雖如此 - 你的意思是使用==
而不是=
?
#include <algorithm> ... std::set_union(s1.begin(), s1.end(), s2.begin(), s2.end() ... )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.