संभव डुप्लिकेट पुनरावृत्ति करते हुए:
Erasing from a std::vector while doing a for each?मिटाएं तत्व एक ही वेक्टर
मैं इस एल्गोरिथ्म के अनुसार रंग vertice लागू करने के लिए कोशिश कर रहा हूँ;
/*
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 जोड़ें।" लाइन लेकिन मुझे लगता है, इसका मतलब है वर्तमान में vol को voling। इसलिए "सेट" क्या है? वैसे भी समस्या यह है कि वेक्टर में तत्व को मिटा रहा है। यह कोड है।
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