generate_n(v1.begin(), v1.size(), generator);
ostream_iterator‹int› iter(cout, ' ');
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
return 0;
}
gener2.cpp
#include ‹stl.h›
#include ‹iostream.h›
#include ‹stdlib.h›
class Fibonacci {
public:
Fibonacci(): v1(0), v2(1) {}
int operator() ();
private:
int v1;
int v2;
};
int Fibonacci::operator() () {
int r = v1 + v2;
v1 = v2;
v2 = r;
return v1;
}
int main() {
vector‹int› v1(10);
Fibonacci generator;
generate(v1.begin(), v1.end(), generator);
ostream_iterator‹int› iter(cout, ' ');
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
return 0;
}
repcpif1.cpp
#include ‹stl.h›
#include ‹iostream.h›
bool odd(int a_) {
return a_ % 2;
}
int main() {
vector‹int› v1(10);
for (int i = 0; i ‹ v1.size(); i++) v1[i] = i % 5;
ostream_iterator‹int› iter(cout, ' ');
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
vector‹int› v2(v1.size());
replace_copy_if(v1.begin(), v1.end(), v2.begin(), odd, 42);
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
copy(v2.begin(), v2.end(), iter);
cout ‹‹ endl;
return 0;
}
setsymd.cpp
#include ‹stl.h›
#include ‹iostream.h›
int v1[3] = {13, 18, 23};
int v2[4] = {10, 13, 17, 23};
int result[4] = {0, 0, 0, 0};
int main() {
set_symmetric_difference(v1, v1 + 3, v2, v2 + 4, result);
for (int i = 0; i ‹ 4; i++) cout ‹‹ result[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
deque1.cpp
#include ‹iostream.h›
#include ‹stl.h›
int main() {
deque‹int› d;
d.push_back(4); // Add after end.
d.push_back(9);
d.push_back(16);
d.push_front(1); // Insert at beginning.
for (int i = 0; i ‹ d.size(); i++) cout ‹‹ 'd[' ‹‹ i ‹‹ '] = ' ‹‹ d[i] ‹‹ endl;