strlensort.cpp
来自「C++&datastructure书籍源码,以前外教提供现在与大家共享」· C++ 代码 · 共 44 行
CPP
44 行
#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 + =
减小字号Ctrl + -
显示快捷键?