words_count_4.cpp

来自「A group of word-analysis examples for C+」· C++ 代码 · 共 36 行

CPP
36
字号
/*
*  Description:
*
*    Report the amount of the defferent words in your input.
*
*  History:
*
*    Initial version created by Royal, Mar. 2004.
*
*  Notes:
*
*    This code has been written to conform to standard C++ and STL. It has been
*    compiled successfully using GNU C++ 3.2, Borland C++ 5.5, and Visual C++ 7.0.
*/

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main()
{
    string word;
    map<string, int> words;
    cout << "Please input words:";
    while (cin >> word) ++words[word];
    map<string, int>::const_iterator it = words.begin();
    map<string, int>::const_iterator end_it = words.end(); 
    while (it != end_it)
    {
        cout << "There are/is " << it->second << " \"" << it->first << "\" in your input." << endl;
        ++it;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?