swap_ranges mit list
-
hallo zusammen,
wieso funktioniert dieser code nicht?
wenn man vector<int> verwendet statt list<int> dann funktioniert das wunderbar.
könnt ihr mir weiterhelfen?
die STL-Algortihmen wie z.b. auch swap_ranges sind doch auf alle STL-Container anwendbar, oder?// swap_ranges example #include <iostream> #include <algorithm> #include <list> using namespace std; int main () { list<int> first (5,10); // first: 10 10 10 10 10 list<int> second (5,33); // second: 33 33 33 33 33 list<int>::iterator it; swap_ranges(first.begin()+1, first.end()-1, second.begin()); // print out results of swap: cout << " first contains:"; for (it=first.begin(); it!=first.end(); ++it) cout << " " << *it; cout << "\nsecond contains:"; for (it=second.begin(); it!=second.end(); ++it) cout << " " << *it; cout << endl;
-
sag doch auch mal, in welcher zeile der erste compilerfehler kommt und welcher fehler es ist.
-
Wie wärs so?
list<int>::iterator begin = first.begin(); advance(begin, +1); list<int>::iterator end = first.end(); advance(end, -1); swap_ranges(begin, end, second.begin());
Du musst std::advance benutzen, da std::list::iterator kein Randdom-Access-Iterator ist.
Gruß
Don06
-
danke Don06, funktioniert jetzt so wie ich es mir vorstelle.
gruss,
DG
-
Don_Gunito schrieb:
wieso funktioniert dieser code nicht?
Ist die denkbar schlechteste Fehlermeldung die du liefern kannst... Ungefähr so sinnvoll wei beim Arzt zu sagen "ich bin krank"