Re: [問題] 文章內搜尋字串
※ 引述《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
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章