[問題] C++ list merge

作者: hardware (哈味)   2015-05-30 17:02:53
開發平台(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
作者: Feis (永遠睡不著 @@)   2015-05-30 17:12:00
不是
作者: bibo9901 (function(){})()   2015-05-30 17:24:00
RTFM
作者: legendmtg (CLANNAD)   2015-05-30 20:42:00
作者: Killercat (殺人貓™)   2015-05-30 21:38:00
接在後面是用insert... merge意義完全不同或者用比較罕用的splice(語意是移動 效能較好)
作者: FierceBreast (兇奴王者)   2015-05-31 01:48:00
原來接後面是 insert 我一直以為是 merge現在才知道+1

Links booklink

Contact Us: admin [ a t ] ucptt.com