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

📄 strlensort.cpp

📁 C++&datastructure书籍源码,以前外教提供现在与大家共享
💻 CPP
字号:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

#include "tvector.h"
#include "sortall.h"
#include "prompt.h"

class StrLenComp
{
  public:
	 int compare(const string& a, const string& b) const
	 // post: return -1/+1/0 as a.length() < b.length()
	 {
	     if (a.length() < b.length()) return -1;
	     if (a.length() > b.length()) return  1;
	     return 0;
	 } 
};
int main()
{
    string word, filename = PromptString("filename: ");
    tvector<string> wvec;
    StrLenComp slencomp;
    int k;
    ifstream input(filename.c_str());
    
    while (input >> word)
    {   wvec.push_back(word);
    }
    InsertSort(wvec, wvec.size(), slencomp);
    
    for(k=0; k < 5; k++)
    {   cout << wvec[k] << endl;
    }
    cout << "-------" << endl << "last words" << endl;
    cout << "-------" << endl;
    for(k=wvec.size()-5; k < wvec.size(); k++)
    {   cout << wvec[k] << endl;
    }
    return 0;
}    

⌨️ 快捷键说明

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