计算文件中的单词数量
How to do it...
#include <iostream> #include <fstream> #include <string> #include <algorithm> #include <iterator> using namespace std;template <typename T> size_t wordcount(T &is) { return distance(istream_iterator<string>{is}, {}); }int main(int argc, char **argv) { size_t wc;if (argc == 2) { ifstream ifs {argv[1]}; wc = wordcount(ifs);} else { wc = wordcount(cin); }cout << "There are " << wc << " words\n"; };$ echo "foo bar baz" | ./count_all_words There are 3 words$ ./count_all_words count_all_words.cpp There are 61 words
How it works...
Last updated