IntVec::iterator iter;
iter = find_if(v.begin(), v.end(), div_3);
if (iter!= v.end())
cout ‹‹ "Value " ‹‹ *iter ‹‹ " at offset " ‹‹ (iter - v.begin()) ‹‹ " is divisible by 3" ‹‹ endl;
return 0;
}
#include ‹iostream.h›
#include ‹math.h›
#include ‹stl.h›
struct square_root: public unary_function‹double, double› {
square_root() {}
double operator() (double x_) const {return sqrt(x_);}
};
int input[3] = {-1, -4, -16};
int main() {
int output[3];
transform(input, input + 3, output, unary_compose‹square_root, negate‹int› ›(square_root(), negate‹int›()));
for (int i = 0; i ‹ 3; i++) cout ‹‹ output[i] ‹‹ endl;
return 0;
}
#include ‹iostream.h›
#include ‹stl.h›
class X {
public:
X(int i_ = 0): i (i_) {}
operator int() const {return i;}
private:
int i;
};
int main() {
os_heap_allocator‹X› a;
// Allocate (but do not construct) storage for 5 elements.
os_heap_allocator‹X›::pointer p = a.allocate(5);
raw_storage_iterator‹X*, X› r(p);
for (int i = 0; i ‹ 5; i++) *r++ = X(i);
for (i = 0; i ‹ 5; i++) cout ‹‹ *p++ ‹‹ endl;
return 0;
}
#include ‹iostream.h›
#include ‹stl.h›
int main() {
set‹int, less‹int› › s;
pair‹set‹int, less‹int› ›::const_iterator, bool› p;
p = s.insert(42);
if (p.second) cout ‹‹ "Inserted new element " ‹‹ *(p.first) ‹‹ endl;
else cout ‹‹ "Existing element = " ‹‹ *(p.first) ‹‹ endl;
p = s.insert(42);
if (p.second) cout ‹‹ "Inserted new element " ‹‹ *(p.first) ‹‹ endl;
else cout ‹‹ "Existing element = " ‹‹ *(p.first) ‹‹ endl;
return 0;
}
#include ‹iostream.h›
#include ‹stl.h›
int array[] = {3, 6, 1, 2, 3, 2, 6, 7, 9};
int main() {
multiset‹int, less‹int› › s(array, array + 9);
multiset‹int, less‹int› ›::iterator i;
// Return location of first element that is not less than 3
i = s.lower_bound(3);
cout ‹‹ "lower bound = " ‹‹ *i ‹‹ endl;
// Return location of first element that is greater than 3
i = s.upper_bound(3);
cout ‹‹ "upper bound = " ‹‹ *i ‹‹ endl;
return 0;
}
#include ‹stl.h›
#include ‹iostream.h›
#include ‹string.h›
bool str_compare(const char* a_, const char* b_) {
return ::strcmp(a_, b_) ‹ 0 ? 1 : 0;
}
char* labels[] = {"aa", "dd", "ff", "jj", "ss", "zz"};
int main() {
const unsigned count = sizeof(labels) / sizeof(labels[0]);
if (binary_search(labels, labels + count, "ff", str_compare)) cout ‹‹ "ff is in labels." ‹‹ endl;
else cout ‹‹ "ff is not in labels." ‹‹ endl;
return 0;
}
#include ‹stl.h›
#include ‹stdlib.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(10);
for (int i = 0; i ‹ v1.size(); i++) v1[i] = rand() % 10;
ostream_iterator‹int› iter(cout, " ");
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
nth_element(v1.begin(), v1.begin() + v1.size() / 2, v1.end(), greater‹int›());
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
return 0;
}
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(10);
iota(v1.begin(), v1.end(), 0);
vector‹int› v2(10);
iota(v2.begin(), v2.end(), 7);
ostream_iterator ‹int› iter(cout, " ");
cout ‹‹ "v1: ";
copy(v1.begin(), v1.end(), iter);
cout ‹‹ "nv2: ";
copy(v2.begin(), v2.end(), iter);
cout ‹‹ endl;
set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), iter);
cout ‹‹ endl;
return 0;
}
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(10);
iota(v1.begin(), v1.end(), 0);
vector‹int› v2(10);
iota(v2.begin(), v2.end(), 7);
ostream_iterator ‹int› iter(cout, " ");
cout ‹‹ "v1: ";
copy(v1.begin(), v1.end(), iter);
cout ‹‹ "nv2: ";
copy(v2.begin(), v2.end(), iter);
cout ‹‹ endl;
set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), iter);
cout ‹‹ endl;
return 0;
}
#include ‹stl.h›
#include ‹iostream.h›
int mult(int a_, int b_) {
return a_ * b_;
}
int main() {
vector‹int› v(10);
for (int i = 0; i ‹ v.size(); i++) v[i] = i + 1;
vector‹int› rslt(v.size());
adjacent_difference(v.begin(), v.end(), rslt.begin(), mult);
ostream_iterator‹int› iter(cout, " ");
copy(v.begin(), v.end(), iter);
cout ‹‹ endl;
copy(rslt.begin(), rslt.end(), iter);
cout ‹‹ endl;
return 0;
}
#include ‹stl.h›
#include ‹iostream.h›
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;
for (int i = 0; i ‹ v1.size(); i++) {
rotate(v1.begin(), v1.begin() + i, v1.end());
ostream_iterator ‹int› iter(cout, " ");
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
}
cout ‹‹ endl;
return 0;
}
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(10);
iota(v1.begin(), v1.end(), 0);
vector‹int› v2(10);
iota(v2.begin(), v2.end(), 7);
ostream_iterator‹int› iter(cout, " ");
cout ‹‹ "v1: ";
copy(v1.begin(), v1.end(), iter);
cout ‹‹ "nv2: ";
copy(v2.begin(), v2.end(), iter);
cout ‹‹ endl;
set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), iter);
cout ‹‹ endl;
return 0;
}
#include ‹iostream.h›
#include ‹stl.h›
char* array1[] = {"laurie", "jennifer", "leisa"};
char* array2[] = {"amanda", "saskia", "carrie"};
int main() {
deque‹char*› names(array1, array1 + 3);
deque‹char*›::iterator i = names.begin() + 2;
copy(array2, array2 + 3, insert_iterator‹deque ‹char*› ›(names, i));
deque‹char*›::iterator j;
for (j = names.begin(); j!= names.end(); j++) cout ‹‹ *j ‹‹ endl;
return 0;
}
#include ‹iostream.h›
#include ‹math.h›
#include ‹stl.h›
struct square_root: public unary_function‹double, double› {
square_root() {}
double operator() (double x_) const {return sqrt(x_);}
};
int input[3] = {-1, -4, -16};
int main() {
int output[3];
transform(input, input + 3, output, compose1(square_root(), negate‹int›()));
for (int i = 0; i ‹ 3; i++) cout ‹‹ output[i] ‹‹ endl;
return 0;
}
#include ‹stl.h›
#include ‹stdlib.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(10);
for (int i = 0; i ‹ v1.size(); i++)
v1[i] = rand() % 10;
ostream_iterator‹int› iter(cout, " ");
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
partial_sort(v1.begin(), v1.begin() + v1.size() / 2, v1.end());
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
return 0;
}
#include ‹stl.h›
#include ‹iostream.h›
bool values_squared(int a_, int b_) {
return (a_ * a_ == b_);
}
int main() {
vector‹int› v1(10);
vector‹int› v2(10);
for (int i = 0; i ‹ v1.size(); i++) {
v1[i] = i;
v2[i] = i * i;
}
if (equal(v1.begin(), v1.end(), v2.begin(), values_squared))
cout ‹‹ "v2[i] == v1[i] * v1[i]" ‹‹ endl;
else cout ‹‹ "v2[i] != v1[i] * v1[i]" ‹‹ endl;
return 0;
}
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(10);
for (int i = 0; i ‹ v1.size(); i++) v1[i] = (v1.size() - i - 1) % 5;
ostream_iterator‹int› iter(cout, " ");
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
inplace_merge(v1.begin(), v1.begin() + 5, v1.end(), greater‹int›());
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
return 0;
}
#include ‹stl.h›
#include ‹stdlib.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(10);
for (int i = 0; i ‹ v1.size(); i++) v1[i] = rand() % 10;
ostream_iterator‹int› iter(cout, " ");
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
nth_element(v1.begin(), v1.begin() + v1.size() / 2, v1.end());
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;