Вход/Регистрация
РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)
вернуться

Менг Ли

Шрифт:

 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;

  • Читать дальше
  • 1
  • ...
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • ...

Ебукер (ebooker) – онлайн-библиотека на русском языке. Книги доступны онлайн, без утомительной регистрации. Огромный выбор и удобный дизайн, позволяющий читать без проблем. Добавляйте сайт в закладки! Все произведения загружаются пользователями: если считаете, что ваши авторские права нарушены – используйте форму обратной связи.

Полезные ссылки

  • Моя полка

Контакты

  • chitat.ebooker@gmail.com

Подпишитесь на рассылку: