[問題] STL sort

看板C_and_CPP作者 (Joy)時間13年前 (2011/02/28 23:09), 編輯推噓2(202)
留言4則, 3人參與, 最新討論串1/2 (看更多)
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Dev C++ 問題(Question): 使用sort()一直有錯誤 ----------------------------------------------------------------------------- class Person { ... private:string name; ... } class PeopleList { public: ... bool PersonComp(Person&,Person&) const; //compare two people's names void Gifting(char*); //gifting operation ... private:int numP; //number of people vector<Person> plistI , plistF ; //people list in vector //type, initial and final } ... bool PeopleList::PersonComp(Person& p1,Person& p2) const { return ( p1.name < p2.name ); //我想用名字去做排序 } void PeopleList::Gifting(char* ifile) { ifstream ifs(ifile); int temp_numP; ifs>>temp_numP; for ( int i=1 ; i<=temp_numP ; i++ ) { string temp_name; ifs>>temp_name; Person *p=new Person(temp_name); Add(*p); } //上面是從檔案裡面先讀取人數,再讀取名字 plistF=plistI; //copy vector std::sort( plistF.begin() , plistF.end() , PersonComp ); //sorting by name //問題就是這個sort,我想練習用STL來做 } ------------------------------------------------------------------------ std::有加沒加都會錯 有加的時候錯誤碼很多行 沒加反而只有三行(同學叫我加加看的) 程式碼(Code):(請善用置底文網頁, 記得排版) 這邊有完整的程式碼 http://codepad.org/LY73NqXU 補充說明(Supplement): 這程式還沒打完 我是邊做邊檢察這樣 假設讀取的檔案是這樣: 5 dave laura owen vick amr 就是有五個人 然後接著讀取他們的名字... 不好意思 第一次在這發文 格式有錯我可以再改 麻煩大家幫忙不才解惑一下 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.5.154

02/28 23:13, , 1F
PersonComp 你做成成員函式了..拉出來到外面然後設個 friend
02/28 23:13, 1F

02/28 23:15, , 2F
或是宣告為 static
02/28 23:15, 2F

02/28 23:25, , 3F
謝謝 我把PersonComp拉出PeopleList外面好像就可以了 不過
02/28 23:25, 3F

02/28 23:26, , 4F
friend是要寫在哪裡、對誰宣告? 還有static要怎麼做呢?
02/28 23:26, 4F
文章代碼(AID): #1DQxh6Ef (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1DQxh6Ef (C_and_CPP)