Re: [問題] 關於operator
※ 引述《tyc5116 (累人啊....)》之銘言:
: ※ 引述《tyc5116 (累人啊....)》之銘言:
: : 推 holymars:用法就直接把物件丟進archieve就好.. ar & obj; 09/14 17:16
: : → tyc5116:那個好像又要再另外安裝,是嗎? 09/14 17:18
: : 推 holymars:對 serialization是boost中少數需要link library的.. 09/14 20:10
: : → tyc5116:那...我研究看看好了,另外,一般的寫法有誰知道嗎? 09/14 20:32
: : → james732:如果你說的是operator>>與<<,應該是寫在全域空間 09/14 20:44
: to j大,我不是很懂您的意思,我想我先我目前的寫法好了
: 一般的list,也就是這樣
: class a{
: vector<int> p;
: ...
public:
friend ostream& operator<<(ostream& out,a& v);
friend istream& operator>>(istream& in,a& v);
: }
: class aaa{
: list<a> v;
public:
: friend ostream& operator<<(ostream& out,aaa& v)
: friend istream& operator>>(istream& in,aaa& v)
: ...
: }
ostream& operator<<(ostream& out,a& v) {
out << v.p.size(); // 先輸出個數,不然讀回來時不知道有幾個
BOOST_FOREACH(int& i, v.p) { // 可以用普通的for迴圈取代
out << i;
}
return out;
}
istream& operator>>(istream& in,a& v) {
int n;
in >> n;
v.p.resize(n);
BOOST_FOREACH(int& i, v.p) {
in >> i;
}
return in;
}
ostream& operator<<(ostream& out,aaa& v) {
out << v.v.size();
BOOST_FOREACH(a& a, v.v) { // 可以用iterator + while迴圈取代
out << a; // 這行會呼叫上面宣告過的 a::operator<<
}
return out;
}
istream& operator>>(istream& in,aaa& v) {
int n;
in >> n;
v.v.resize(n);
BOOST_FOREACH(a& a, v.v) {
in >> a;
}
return in;
}
最後就是在aaa裡定義 SaveContet() LoadContent()之類的function
去呼叫aaa::operator<< 和aaa::operator>>
事實上你要做的事和Boost::serialization想要解決的問題一模一樣
用Boost::serialization來寫 會省很多功夫
至少你不用自己處理STL container
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.32.13.219
※ 編輯: holymars 來自: 114.32.13.219 (09/15 01:05)
※ 編輯: holymars 來自: 114.32.15.163 (09/15 17:13)
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章