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

📄 words_maxmin_2.cpp

📁 A group of word-analysis examples for C++/STL novices
💻 CPP
字号:
/*
*  Description:
*
*    Report the longest and the shortest words in your input.
*
*  History:
*
*    Initial version created by Royal, May, 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;

vector<string> read(istream& ism);
void maxmin(const vector<string>& words);

int main()
{
  cout << "please input words:";
  vector<string> words = read(cin);
  maxmin(words);
}

vector<string> read(istream& ism)
{ 
    string s;
    vector<string> words;
    while(ism >> s) words.push_back(s);
    return words;
}

void maxmin(const vector<string>& words)
{
    string maxword, minword;
    string::size_type maxsize(0), minsize(100), len;   
    for( vector<string>::const_iterator iter = words.begin(); iter != words.end(); iter++)
    {
        len = (*iter).size();
        if(len >= maxsize)
        {
            maxsize = len;
            maxword = *iter;
        }
        else if(len <= minsize)
        {
            minsize = len;
            minword = *iter;
        }
    }
    cout << "The longest word is \"" << maxword << "\"; it's length is " << maxsize << endl;
    cout << "The shortest word is \"" << minword << "\"; it's length is " << minsize << endl;
}

⌨️ 快捷键说明

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