分类: C/C++
2010-03-22 13:45:36
int main()
{
// Seed the random number generator
srandom( time(NULL) );
// Construct vector and fill with random integer values
vectorcollection(10);
for (int i = 0; i < 10; i++)
collection[i] = random() % 10000;;
// Display, sort, and redisplay
Display(collection, "Before sorting");
sort(collection.begin(), collection.end());
Display(collection, "After sorting");
return 0;
}
// Display label s and contents of integer vector v
void Display(vector& v, const char* s) {
cout << endl << s << endl;
copy(v.begin(), v.end(),
ostream_iterator(cout, "\t")); cout << endl;
}
copy(v.begin(), v.end(),
ostream_iterator(cout, "\t"));
$ g++ outstrm.cpp
$ ./a.out
Before sorting
677 722 686 238 964 397 251 118 11 312
After sorting
11 118 238 251 312 397 677 686 722 964
ostream_iterator(cout, "\n")