Руководство по стандартной библиотеке шаблонов STL


Istmit2


#include <iostream.h> #include <fstream.h> #include <stl.h>

typedef vector<char> Line;

void printLine (const Line* line_) { vector<char>::const_iterator i; for (i = line_->begin (); i != line_->end (); i++) cout << *i; cout << endl; }

int main () { Line buffer; vector<Line*> lines; ifstream s ("data.txt"); s.unsetf (ios::skipws); // Disable white-space skipping. istream_iterator<char, ptrdiff_t> it1 (s); // Position at start of file. istream_iterator<char, ptrdiff_t> it2; // Serves as "past-the-end" marker. copy (it1, it2, back_inserter (buffer)); Line::iterator i = buffer.begin (); Line::iterator p; while (i != buffer.end ()) { p = find (i, buffer.end (), '\n'); lines.push_back (new Line (i, p)); i = ++p; } sort (lines.begin (), lines.end (), less_p<Line*> ()); cout << "Read " << lines.size () << " lines" << endl; vector<Line*>::iterator j; for (j = lines.begin (); j != lines.end (); j++) printLine (*j); release (lines.begin (), lines.end ()); // Release memory. return 0; }




Начало  Назад  Вперед



Книжный магазин