3-4.cpp

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

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

int main()
{
	cout << "Please enter in some words:" << endl;
	string word;
	vector<string> words;
	
	while (cin >> word)
	words.push_back(word);
	
	vector<double>::size_type value = words.size();
	
	string::size_type maxsize = words[0].size();
	int max = 0;
	for(int n = 0;n != value - 1;n++) {
		if(words[n].size() > maxsize) {
		   maxsize =  words[n].size();
		   max = n;
        }
    } 
	string::size_type minsize = words[0].size();
	int min = 0;
	for(int m = 0;m != value - 1;m++) {
		if(words[m].size() < minsize) {
		   minsize = words[m].size();
		   min = m;
       	}
    }
	cout << endl;
	
	cout << "The longest word is " << words[max] << '.'
	     << "The length is " << maxsize << endl;
	cout << "The shortest word is " << words[min] << '.'
	     << "The length is " << minsize << endl;
	
	return 0;
} 		

⌨️ 快捷键说明

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