[問題] C++ list merge

看板C_and_CPP作者 (硬體)時間9年前 (2015/05/30 17:02), 編輯推噓2(205)
留言7則, 5人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) G++ Linux 問題(Question): L.merge(L2) 這個意思不是將 L2 的資料 接在 L後面嗎? 但是我跑起來不是這樣 程式碼(Code):(請善用置底文網頁, 記得排版) #include <list> #include <stdlib.h> #include <stdio.h> #include <iostream> #include <string> #include <fcntl.h> #include <sys/time.h> #include <unistd.h> using namespace std; class AAA { friend ostream &operator<<(ostream &, const AAA &); public: char x; int y; float z; AAA(); AAA(const AAA &); ~AAA(){}; AAA &operator=(const AAA &rhs); int operator==(const AAA &rhs) const; int operator<(const AAA &rhs) const; }; AAA::AAA() // Constructor { x = 'C'; y = 0; z = 0; } AAA::AAA(const AAA &copyin) // Copy constructor to handle pass by value. { x = copyin.x; y = copyin.y; z = copyin.z; } ostream &operator<<(ostream &output, const AAA &aaa) { output << aaa.x << ' ' << aaa.y << ' ' << aaa.z << endl; return output; } AAA& AAA::operator=(const AAA &rhs) { this->x = rhs.x; this->y = rhs.y; this->z = rhs.z; return *this; } int AAA::operator==(const AAA &rhs) const { if( this->x != rhs.x) return 0; if( this->y != rhs.y) return 0; if( this->z != rhs.z) return 0; return 1; } // This function is required for built-in STL list functions like sort int AAA::operator<(const AAA &rhs) const { if( this->x == rhs.x && this->y == rhs.y && this->z < rhs.z) return 1; if( this->x == rhs.x && this->y < rhs.y) return 1; if( this->x < rhs.x ) return 1; return 0; } main() { list<AAA> L , L2; AAA Ablob ,louis; begin: list<AAA>::iterator i; Ablob.x='C'; Ablob.y=2; Ablob.z=4.2355; L.push_back(Ablob); // Insert a new element at the end Ablob.x='Z'; L.push_back(Ablob); // Object passed by value. Uses default member-wise // copy constructor Ablob.z=3.2355; L.push_back(Ablob); Ablob.x='H'; Ablob.y=7; Ablob.z=7.2355; L.push_back(Ablob); louis.x='K'; louis.y=8; louis.z=9.25452; L2.push_back(louis); louis.x='B'; louis.y=8; louis.z=5.25452; L2.push_back(louis); //for(i=L.begin(); i != L.end(); ++i) cout << (*i).x << " "; // print member //cout << endl; cout << endl; cout<<"Unsort:"<<endl; for(i=L.begin(); i != L.end(); ++i) cout << *i ; // print with overloaded operator cout << endl; cout << endl; cout<<"Unsort:"<<endl; for(i=L2.begin(); i != L2.end(); ++i) cout << *i ; // print with overloaded operator cout << endl; L2.merge(L); cout << endl; cout<<"after merge:"<<endl; for(i=L2.begin(); i != L2.end(); ++i) cout << *i <<""; // print with overloaded operator cout << endl; //L.erase(++L.begin()); cout<< "L.size:"<<L.size()<<endl; cout<< "L.empty:"<<L.empty()<<endl; cout << endl; cout << "Sorted: " << endl; L.sort(); for(i=L.begin(); i != L.end(); ++i) cout << *i ; // print with overloaded operator cout << endl; cout<<"after stored:"<<endl; for(i=L.begin(); i != L.end(); ++i) cout << *i << " "; // print with overloaded operator cout << endl; cout<< "L.size:"<<L.size()<<endl; cout<< "L.empty:"<<L.empty()<<endl; /* if(L.empty()) { goto begin; } */ return 0; } 補充說明(Supplement): 結果是 Unsort: C 2 4.2355 Z 2 4.2355 Z 2 3.2355 H 7 7.2355 Unsort: K 8 9.25452 B 8 5.25452 after merge: C 2 4.2355 K 8 9.25452 B 8 5.25452 Z 2 4.2355 Z 2 3.2355 H 7 7.2355 這裡錯了@@ 是哪邊的原因會錯嗎 謝謝 L.size:0 L.empty:1 Sorted: after stored: L.size:0 L.empty:1 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.117.89.67 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1432976576.A.A8F.html

05/30 17:12, , 1F
不是
05/30 17:12, 1F

05/30 17:24, , 2F
RTFM
05/30 17:24, 2F

05/30 21:38, , 4F
接在後面是用insert... merge意義完全不同
05/30 21:38, 4F

05/30 21:39, , 5F
或者用比較罕用的splice(語意是移動 效能較好)
05/30 21:39, 5F

05/31 01:48, , 6F
原來接後面是 insert 我一直以為是 merge
05/31 01:48, 6F

05/31 01:48, , 7F
現在才知道+1
05/31 01:48, 7F
文章代碼(AID): #1LQNp0gF (C_and_CPP)