cout ‹‹ endl;
d.pop_front(); // Erase first element.
d[2] = 25; // Replace last element.
for (i = 0; i ‹ d.size(); i++)
cout ‹‹ 'd[' ‹‹ i ‹‹ '] = ' ‹‹ d[i] ‹‹ endl;
return 0;
}
findif1.cpp
#include ‹stl.h›
#include ‹iostream.h›
bool div_3(int a_) {
return a_ % 3 ? 0 : 1;
}
int main() {
typedef vector‹int› IntVec;
IntVec v(10);
for (int i = 0; i ‹ v.size(); i++) v[i] = (i + 1) * (i + 1);
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;
}
ucompos1.cpp
#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;
}
rawiter.cpp
#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;
}
set2.cpp
#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;
}