OPerator overloading

Hi,

Couple of times i am stucked with Operator overloading techniques, Can any one throw little more basic and advanced features required to handle this concept in a practical way, by giving some advanced examples.
You just need to read the <<C++ Primer 4th>>

The is the example code ,you can read the code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/********************************************************************
	created:	2008/07/01
	created:	1:7:2008   12:49
	filename: 	f:\coding\C++\CSDN_SubList\CSDN_SubList\CSDN_SubList.cpp
	file path:	f:\coding\C++\CSDN_SubList\CSDN_SubList
	file base:	CSDN_SubList
	file ext:	cpp
	author:		hecan
	
	purpose:	
	test data "data.txt"

	1 2 3 
	1 4 5
	1 6 7 
	2 4 5 
	2 6 7 
	2 2 3 
    
*********************************************************************/
/************************************************************************/
/* =============WinXP Sp2 + VS2003.net Pass==========================*/
/************************************************************************/

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <list>
#include <algorithm>
using namespace std;
class CData
{
public:
	CData(int _m_iA, int _m_iB, int _m_iC):
		 m_iA(_m_iA), m_iB(_m_iB), m_iC(_m_iC)
		 {

		 }
	int getA()
	{
		return m_iA;
	}
	int getB()
	{
		return m_iB;
	}
	int getC()
	{
		return m_iC;
	}

	 CData()
	 {
	 }

	 bool operator < ( CData &back)
	 {
		 return (m_iA < back.getA());
      }

		 
	 bool operator == ( CData &back)
	 {
		 return (m_iA == back.getA());
	 }

	 CData & operator = (CData & cdCopyData)
	 {
		m_iA =  cdCopyData.getA();
		m_iB = cdCopyData.getB();
		m_iC = cdCopyData.getC();	
		return *this;
	 }
		

	 friend ostream & operator <<(ostream & os, const CData &data)
	 {
		 os << data.m_iA << " "
			 << data.m_iB << " "
			 << data.m_iC << endl;
			 return os;
	}
public:
	int m_iA;
	int m_iB;	
	int m_iC;

};


int main()
{
	list< list< CData > > llData;
	list< CData > lDataTmp;
	ifstream ifsFile("data.txt", ios::in);

	if (!ifsFile)
	{
		cerr << "Error!" <<endl;
	}

    string sLine;	
	//取出所有数据
	while (getline(ifsFile, sLine))
	{
		istringstream issLine(sLine);
		int a, b, c;		
	issLine >> a >> b >> c;
	CData tmp(a, b, c);
	lDataTmp.push_back(tmp);	
	}


lDataTmp.sort();
cout<<"排序后的原始数据:"<<endl;
copy(lDataTmp.begin(), lDataTmp.end(), ostream_iterator< CData >(cout));
cout<<endl;

list< CData > lDataTmpCopy(lDataTmp);//生成一份拷贝用来得到一份唯一的数据

lDataTmpCopy.unique();
list< CData > ld_Tmp2(lDataTmpCopy);
	//list< CData > ld_Tmp2(lDataTmp.begin(), unique(lDataTmp.begin(), lDataTmp.end()));
cout<<"去掉重复a的数据:"<<endl;
copy(ld_Tmp2.begin(), ld_Tmp2.end(), ostream_iterator< CData >(cout));
cout<<endl;

//////////找到list中a相同的做成一个list插入到以list为节点的list中///////////
while (!ld_Tmp2.empty())
{
	list< CData >::iterator front_Iter = find_first_of(lDataTmp.begin(), lDataTmp.end(),
					ld_Tmp2.begin(), ld_Tmp2.end());
	list< CData >::iterator back_Iter = find_end(lDataTmp.begin(), lDataTmp.end(),
		ld_Tmp2.begin(), ld_Tmp2.end());
	//链表中的每个节点
	list<CData> nodeData(front_Iter, ++back_Iter);

	llData.push_back(nodeData);

	ld_Tmp2.erase(ld_Tmp2.begin());
}


////////////////////////==test the result==/////////////////////////////////////////////////////////////////
cout <<"用来显示形成链表后的数据:" << endl;
for (list< list< CData > >::iterator LC_Iter = llData.begin(); LC_Iter != llData.end(); ++LC_Iter)
{
	copy(LC_Iter->begin(), LC_Iter->end(), ostream_iterator< CData >(cout));
}

return 0;
}

HI,
Thanks for the code, but what does this means?
"You just need to read the <<C++ Primer 4th>>"
<<C++ Primer 4th>> is a bible about the c++ wrote by Stanley B. Lippman...
OK, Thanks.
Topic archived. No new replies allowed.