[问题] 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