编辑代码

#include<istream>
#include<iostream>
#include<vector>
#include<utility>
#include<iterator>
#include<algorithm>
using namespace std;
struct strtab_cmp
{
	typedef vector<char>::iterator  strab_iterator;
	//仿函数
	bool operator()(const pair<strab_iterator, strab_iterator>&x, const pair<strab_iterator, strab_iterator>&y) const
	{    //字典排序
		return lexicographical_compare(x.first, x.second, y.first, y.second);
	}
};
 
struct strtab_print
{
	ostream &out;
	strtab_print(ostream &os) :out(os){}
 
	typedef vector<char>::iterator strab_iterator;
 
	void operator()(const pair<strab_iterator, strab_iterator>&s)  const
	{                             //输出流...
		copy(s.first, s.second, ostream_iterator<char>(out));
	}
 
};
 
int main()
{
	vector<char> strtab;//保存所有字符
	char c;
	while (cin.get(c))
	{
		strtab.push_back(c);
	}
	typedef vector<char>::iterator strab_iterator;
 
	vector<pair<strab_iterator, strab_iterator>>lines;
	strab_iterator start = strtab.begin();
	while (start != strtab.end())
	{
		strab_iterator next = find(start, strtab.end(), '\n');//遇到回车符就返回其下一个
		if (next != strtab.end())
			++next; 
                      //将一行的首尾指针放入line中
		lines.push_back(make_pair (start, next));
		start = next;
	}
 
	sort(lines.begin(), lines.end(), strtab_cmp());
 
	for_each(lines.begin(), lines.end(), strtab_print(cout));
	
	system("pause");
}