int main() {
pair ‹int*, int*› range;
range = equal_range(numbers, numbers + 10, 2);
cout ‹‹ '2 can be inserted from before index ' ‹‹ (range.first - numbers)
‹‹ ' to before index ' ‹‹ (range.second - numbers) ‹‹ endl;
return 0;
}
advance.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 = v.begin();
cout ‹‹ 'At Beginning: ' ‹‹ *location ‹‹ endl;
advance(location, 5);
cout ‹‹ 'At Beginning + 5: ' ‹‹ *location ‹‹ endl;
return 0;
}
replace1.cpp
#include ‹stl.h›
#include ‹iostream.h›
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;
replace(v1.begin(), v1.end(), 2, 42);
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
return 0;
}
alg3.cpp
#include ‹iostream.h›
#include ‹stl.h›
int main() {
vector‹int› i;
i.push_back(1);
i.push_back(4);
i.push_back(2);
i.push_back(8);
i.push_back(2);
i.push_back(2);
int n = 0; // Must be initialized, as count increments n.
count(i.begin(), i.end(), 2, n);
cout ‹‹ 'Count of 2s = ' ‹‹ n ‹‹ endl;
return 0;
}
func2.cpp
#include ‹iostream.h›
#include ‹stl.h›
bool bigger_than(int x_, int y_) {
return x_ › y_;
}
int main() {
vector‹int›v;
v.push_back(4);
v.push_back(1);
v.push_back(5);
sort(v.begin(), v.end(), bigger_than);
vector‹int›::iterator i;
for (i = v.begin(); i != v.end(); i++) cout ‹‹ *i ‹‹ endl;
return 0;
}
unegate1.cpp
#include ‹iostream.h›
#include ‹stl.h›
struct odd: public unary_function‹int, bool› {
odd() {}
bool operator() (int n_) const {return (n_ % 2) - 1;}