Re: [問題] 文章內搜尋字串

看板C_and_CPP (C/C++)作者 (CA)時間16年前 (2009/03/29 23:45), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/3 (看更多)
※ 引述《cacaliang (caca)》之銘言: : 想要在txt檔,文章字數不定的文章中, : 搜尋一個字串,並看它出現幾次。 : 目前我幼兒般的程度只有辦法寫成這樣~ using namespace std; struct Compare { Compare( const string & s ) : s_( s ) {} bool operator() ( const string & that ) { return that.find( this->s_ ) != string::npos; } private: string s_; }; int countWordInFile( istream & in, const string & s ) { return count_if( istream_iterator< string >( in ), istream_iterator< string >(), Compare( s ) ); } ---- 假設 fin 是你開的檔, keyword 是你要找的字串 用 countWordInFile( fin, keyword ); 就會回傳 keyword 出現的次數 不過是"包含", 不是"完全相等", 也就是說 "today" 會 match "day" ---- ....好啦, 其實沒什麼幫助 = = -- 自High筆記(半荒廢) http://legnaleurc.blogspot.com/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.146.194.91
文章代碼(AID): #19pvU1sa (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #19pvU1sa (C_and_CPP)