class line_iterator
{
istream* in;
string line;
bool is_valid;
void read()
{
if (*in)
getline(*in, line);
is_valid = (*in) ? true : false;
}
public:
typedef input_iterator_tag iterator_category;
typedef string value_type;
typedef ptrdiff_t difference_type;
typedef const string* pointer;
typedef const string& reference;
public:
line_iterator() : in(&cin), is_valid(false){}
line_iterator(istream& s) : in(&s){ read(); }
reference operator*() const { return line; }
pointer operator->() const {return &line; }
line_iterator operator++()
{
read();
return *this;
}
line_iterator operator++(int)
{
line_iterator tmp = *this;
read();
return tmp;
}
bool operator==(const line_iterator& i)const
{
return (in == i.in && is_valid == i.is_valid) ||
(is_valid == false && i.is_valid == false);
}
bool operator != (const line_iterator& i) const
{
return !(*this == i);
}
};
//测试
int _tmain(int argc, _TCHAR* argv[])
{
line_iterator iter(cin);
line_iterator end_of_file;
vector<string> V(iter, end_of_file);
sort(V.begin(), V.end());
copy(V.begin(), V.end(), ostream_iterator<string>(cout, "\n"));
return 0;
}
posted on 2008-02-04 13:48
RUI 阅读(253)
评论(0) 编辑 收藏 引用