prog20_12.cpp
来自「c++最经典的入门书籍」· C++ 代码 · 共 35 行
CPP
35 行
// Program 20.12 A simple word collocation File: prog20_12.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <map>
using std::cout;
using std::endl;
using std::string;
const string twister =
"How much wood would a woodchuck chuck if a woodchuck "
"could chuck wood? A woodchuck would chuck as much wood "
"as a woodchuck could chuck if a woodchuck could chuck wood.";
int main() {
typedef std::map<string, int> Collocation; // Type for our map
typedef Collocation::const_iterator WordIter; // Iterator type for our map
Collocation words; // Map to store words and word counts
std::istringstream text(twister); // Text string as a stream
std::istream_iterator<string> begin(text); // Stream iterator
std::istream_iterator<string> end; // End stream iterator
for( ; begin != end ; ++begin) // Iterate over the words in the stream
words[*begin]++; // Store and increment word count
// Ouput the words and their counts
for(WordIter iter = words.begin() ; iter != words.end() ; ++iter)
cout << std::setw(6) << iter->second << " " << iter->first << endl;
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?