Шрифт:
#include ‹stl.h›
#include ‹stdlib.h›
#include ‹iostream.h›
class MyRandomGenerator {
public:
nsigned long operator(unsigned long n_);
};
unsigned long MyRandomGenerator::operator(unsigned long n_) {
return rand % n_;
}
int main {
vector‹int› v1(10);
iota(v1.begin, v1.end, 0);
ostream_iterator ‹int› iter(cout, " ");
copy(v1.begin, v1.end, iter);
cout ‹‹ endl;
MyRandomGenerator r;
for (int i = 0; i ‹ 3; i++) {
random_shuffle(v1.begin, v1.end, r);
copy(v1.begin, v1.end, iter);
cout ‹‹ endl;
}
return 0;
}
merge2.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main {
vector‹int› v1(5);
vector‹int› v2(v1.size);
for (int i = 0; i ‹ v1.size; i++) {
v1[i] = 10 - i;
v2[i] = 7 - i;
}
vector‹int› result(v1.size + v2.size);
merge(v1.begin, v1.end, v2.begin, v2.end, result.begin, greater‹int›);
ostream_iterator ‹int› iter(cout, " ");
copy(v1.begin, v1.end, iter);
cout ‹‹ endl;
copy(v2.begin, v2.end, iter);
cout ‹‹ endl;
copy(result.begin, result.end, iter);
cout ‹‹ endl;
return 0;
}
adjfind1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main {
typedef vector‹int› IntVector;
IntVector v(10);
for (int i = 0; i ‹ v.size; i++) v[i] = i;
IntVector::iterator location;
location = adjacent_find(v.begin, v.end);
if (location != v.end) cout ‹‹ "Found adjacent pair of: " ‹‹ *location ‹‹ endl;
else cout ‹‹ "No adjacent pairs" ‹‹ endl;
v[6] = 7;
location = adjacent_find(v.begin, v.end);
if (location!= v.end) cout ‹‹ "Found adjacent pair of: " ‹‹ *location ‹‹ endl;
else cout ‹‹ "No adjacent pairs" ‹‹ endl;
return 0;
}
vec7.cpp
#include ‹iostream.h›
#include ‹stl.h›
int array1[] = {1, 4, 25};
int array2[] = {9, 16};
int main {
vector‹int› v(array1, array1 + 3);
v.insert(v.begin, 0); // Insert before first element.
v.insert(v.end, 36); // Insert after last element.
for (int i = 0; i ‹ v.size; i++) cout ‹‹ "v[" ‹‹ i ‹‹ "] = " ‹‹ v[i] ‹‹ endl;
cout ‹‹ endl;
// Insert contents of array2 before fourth element.
v.insert(v.begin + 3, array2, array2 + 2);
for (i = 0; i ‹ v.size; i++)
cout ‹‹ "v[" ‹‹ i ‹‹ "] = " ‹‹ v[i] ‹‹ endl;
cout ‹‹ endl;
return 0;
}
bcompos1.cpp
#include ‹iostream.h›
#include ‹stl.h›
struct odd: public unary_function‹int, bool› {
odd {}
bool operator (int n_) const {return (n_ % 2) - 1;}