所以我正在建立一个读书的程序,并计算特定单词出现在其中的次数。但是,如果我试着逐字逐句地阅读"使用空格分隔符,它也会"," "." and "double whitespaces"
进入我的结果。我该如何解决这个问题?如果我不必使用运算符,它会更容易。
代码:
class Wordcounter
{
private:
struct Data{
string word;
int n;
} d;
vector<Data> w;
public:
friend istream&operator >>(istream&in, Wordcounter&Wordcounter){
string junk;
getline(in, Wordcounter.d.word, ' '); //Delimiter.
/***************** I've tried something like this *********************
if(Wordcounter.d.word.compare(".") != 0){
getline(in, Wordcounter.d.word, ' ');
} */
return in;
}
friend ostream&operator << (ostream&out, Wordcounter&Wordcounter) {
out << Wordcounter.d.word << endl;
return out;
}
void read_file(string filename){
ifstream myfile;
myfile.open(filename);
Wordcounter wc;
while(myfile>>wc){
wc.w.push_back(wc.d);
cout << wc;
}
myfile.close();
}
Wordcounter(){
}
~Wordcounter(){
}
};