3-3.cpp

来自「Accelerted cpp 第三章答案 (部分) 一本很好的cpp入门书」· C++ 代码 · 共 42 行

CPP
42
字号
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using std::cin; 	using std::cout;
using std::endl;	using std::string;
using std::sort;    using std::vector;

int main()
{
	cout << "Please enter some words:" <<endl;
	string word;
	vector<string> words;
	
	while(cin >> word)
	words.push_back(word);
	cout << endl;

	sort(words.begin(),words.end());
	vector<double>::size_type number = words.size();
	
	if (number == 0) {
	    cout << "You didn't enter any words."
		    "Please try again."<<endl;
	    return 1;
	    }	
	
	int count = 1;
	int n = 0;
	while(n != number) {
	    n ++;
	    if(words[n] == words[n - 1]) 
	       count ++;	
	    else {
	       cout << "The string \"" << words[n-1] << "\" emerges " 
                    << count << "times." <<endl;
		    count = 1;
		 }
    }
	return 0;
}

⌨️ 快捷键说明

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