可能性の重複同じベクトルを反復しながら:
Erasing from a std::vector while doing a for each?消去要素
を私は、このアルゴリズムに従って着色頂点を実装しようとしています。
/*
Given G=(V,E):
Compute Degree(v) for all v in V.
Set uncolored = V sorted in decreasing order of Degree(v).
set currentColor = 0.
while there are uncolored nodes:
set A=first element of uncolored
remove A from uncolored
set Color(A) = currentColor
set coloredWithCurrent = {A}
for each v in uncolored:
if v is not adjacent to anything in coloredWithCurrent:
set Color(v)=currentColor.
add v to currentColor.
remove v from uncolored.
end if
end for
currentColor = currentColor + 1.
end while
*/
「vをcurrentColorに追加する」とは分かりません。しかし、私は、それはcurrentColorをvにしようとしていることを意味します。したがって、 "set"は何ですか?とにかく問題は、反復処理中にベクトルの要素を消去することです。これがコードです。
vector<struct Uncolored> uc;
vector<struct Colored> c;
int currentColor = 0;
struct Colored A;
struct Colored B;
vector<struct Uncolored>::iterator it;
vector<struct Uncolored>::iterator it2;
vector<struct Colored>::iterator it3;
for(it=uc.begin();it<uc.end();it++){
A.id = (*it).id;
uc.erase(uc.begin());
A.color = currentColor;
c.push_back(A);
for(it2=uc.begin();it2<uc.end();it2++) {
it3=c.begin();
while(it3 != c.end()) {
if(adjacencyMatris[(*it2).id][(*it3).id] == 0) {
B.id = (*it2).id;
it2 = uc.erase(it2);
B.color = currentColor;
c.push_back(B);
}
it3++;
}
}
currentColor = currentColor + 1;
}
私はit2 = uc.erase(it2);
行はすでに一般的に使用されていますが、実行時エラーが発生すると考えています。
あなたは私たちにどのようなエラーを伝えることはできますか? – vidit