#include<iostream> #include <algorithm> #include <vector> using namespace std; struct strtab_cmp { typedef vector<char>::iterator strtab_iterator; //以pair的first和second标明一行文本,进行字典编纂式的比较 bool operator()(const pair<strtab_iterator,strtab_iterator>&x,const pair<strtab_iterator,strtab_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 strtab_iterator; //定义输出 void operator ()(const pair<strtab_iterator,strtab_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 strtab_iterator; //声明指针pair vector<pair<strtab_iterator,strtab_iterator> > lines; strtab_iterator start=strtab.begin(); while (start!=strtab.end()) { strtab_iterator next=find(start,strtab.end(),'/n'); if(next!=strtab.end()) ++next; 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));//输出 return 0; } Andy于2010-07-09 09:26:22