⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 words_count_2.cpp

📁 A group of word-analysis examples for C++/STL novices
💻 CPP
字号:
/*
*  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 <vector>

using namespace std;

bool output_already(const string& word, const vector<string>& output_words);

int main()
{
    string word;
    vector<string> words;
    vector<string> output_words;
      
    cout << "Please input words:";
    while(cin >> word) words.push_back(word);
    
    vector<string>::const_iterator it = words.begin();
    vector<string>::const_iterator end_it = words.end();       
    while (it != end_it)    
    {
        if (output_already(*it, output_words))
        {
            ++it;    // notice!
            continue;
        }              
        int count = 0;       
        for(vector<string>::const_iterator it2 = words.begin(); it2 != words.end(); ++it2)
            if(*it == *it2) ++count;

        cout << "There are/is " << count << " \"" << *it << "\" in your input." << endl;
        output_words.push_back(*it);
        
        ++it;
    }
}

bool output_already(const string& word, const vector<string>& output_words)
{
    vector<string>::const_iterator it = output_words.begin();
    vector<string>::const_iterator end_it = output_words.end(); 
    while (it != end_it)
    {
        if(word == *it) return true;
        ++it;
    }
    return false;
}  

⌨️ 快捷键说明

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