作者legnaleurc (CA)
看板C_and_CPP
标题Re: [问题] 文章内搜寻字串
时间Sun Mar 29 23:45:02 2009
※ 引述《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