Выбрать главу

equal0.cpp

#include ‹stl.h›

#include ‹iostream.h›

int numbers1[5] = {1, 2, 3, 4, 5};

int numbers2[5] = {1, 2, 4, 8, 16};

int numbers3[2] = {1, 2};

int main() {

 if (equal(numbers1, numbers1 + 5, numbers2))

  cout ‹‹ "numbers1 is equal to numbers2" ‹‹ endl;

 else cout ‹‹ "numbers1 is not equal to numbers2" ‹‹ endl;

 if (equal(numbers3, numbers3 + 2, numbers1))

  cout ‹‹ "numbers3 is equal to numbers1" ‹‹ endl;

 else cout ‹‹ "numbers3 is not equal to numbers1" ‹‹ endl;

 return 0;

}

genern2.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_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;

 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;

}

mset3.cpp

#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;

}

binsrch2.cpp

#include ‹stl.h›

#include ‹iostream.h›