`
Читать книги » Книги » Компьютеры и Интернет » Программирование » Александр Степанов - РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)

Александр Степанов - РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)

1 ... 20 21 22 23 24 ... 30 ВПЕРЕД
Перейти на страницу:

cout ‹‹ "nis: " ‹‹ result ‹‹ endl;

return 0;

}

merge1.cpp

#include ‹stl.h›

#include ‹iostream.h›

int main() {

 vector‹int› v1(5);

 vector‹int› v2(v1.size());

 iota(v1.begin(), v1.end(), 0);

 iota(v2.begin(), v2.end(), 3);

 vector‹int› result(v1.size() + v2.size());

 merge(v1.begin(), v1.end(), v2.begin(), v2.end(), result.begin());

 ostream_iterator‹int› iter(cout, " ");

 copy(v1.begin(), v1.end(), iter);

 cout ‹‹ endl;

 copy(v2.begin(), v2.end(), iter);

 cout ‹‹ endl;

 copy(result.begin(), result.end(), iter);

 cout ‹‹ endl;

 return 0;

}

bcompos2.cpp

#include ‹iostream.h›

#include ‹stl.h›

struct odd: public unary_function‹int, bool› {

 odd() {}

 bool operator() (int n_) const  {return (n_ % 2) - 1;}

};

struct positive: public unary_function‹int, bool› {

 positive() {}

 bool operator() (int n_) const  {return n_ ›= 0;}

};

int array[6] = {-2, -1, 0, 1, 2, 3};

int main() {

 int* p = find_if(array, array + 6, compose2(logical_and‹bool›(), odd(), positive()));

 if (p != array + 6) cout ‹‹ *p ‹‹ " is odd and positive" ‹‹ endl;

 return 0;

}

error3.cpp

#include ‹stl.h›

// Compile this code without defining OS_USE_EXCEPTIONS.

void my_handler(int code_, const char* str_) {

 cout ‹‹ "Caught " ‹‹ str_ ‹‹ "[code " ‹‹ code_ ‹‹ "]" ‹‹ endl;

}

int main() {

 os_handler_function_t old_h = os_set_error_handler(my_handler);

 vector‹int› v;

 v.pop_back(); // Generates an empty object error.

 cout ‹‹ "returned from pop_back()" ‹‹ endl;

 os_set_error_handler(old_h);

 v.pop_back(); // Generates an empty object error.

 cout ‹‹ "successful termination" ‹‹ endl;

 return 0;

}

incl0.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] = {4, 8};

int main() {

 if (includes(numbers1, numbers1 + 5, numbers3, numbers3 + 2))

  cout ‹‹ "numbers1 includes numbers3" ‹‹ endl;

 else cout ‹‹ "numbers1 does not include numbers3" ‹‹ endl;

 if (includes(numbers2, numbers2 + 5, numbers3, numbers3 + 2))

  cout ‹‹ "numbers2 includes numbers3" ‹‹ endl;

 else cout ‹‹ "numbers2 does not include numbers3" ‹‹ endl;

 return 0;

}

setdiff2.cpp

#include ‹stl.h›

#include ‹iostream.h›

#include ‹string.h›

char* word1 = "ABCDEFGHIJKLMNO";

char* word2 = "LMNOPQRSTUVWXYZ";

int main() {

 ostream_iterator‹char› iter(cout, " ");

 cout ‹‹ "word1: ";

 copy(word1, word1 + ::strlen(word1), iter);

 cout ‹‹ "nword2: ";

 copy(word2, word2 + ::strlen(word2), iter);

 cout ‹‹ endl;

 set_difference(word1, word1 + ::strlen(word1),  word2, word2 + ::strlen(word2), iter, less‹char›());

 cout ‹‹ endl;

 return 0;

}

setunon2.cpp

#include ‹stl.h›

#include ‹iostream.h›

#include ‹string.h›

char* word1 = "ABCDEFGHIJKLMNO";

char* word2 = "LMNOPQRSTUVWXYZ";

int main() {

 ostream_iterator‹char› iter(cout, " ");

 cout ‹‹ "word1: ";

 copy(word1, word1 + ::strlen(word1), iter);

 cout ‹‹ "nword2: ";

 copy(word2, word2 + ::strlen(word2), iter);

 cout ‹‹ endl;

 set_union(word1, word1 + ::strlen(word1),  word2, word2 + ::strlen(word2), iter, less‹char›());

 cout ‹‹ endl;

 return 0;

}

unique2.cpp

#include ‹stl.h›

#include ‹iostream.h›

#include ‹string.h›

bool str_equal(const char* a_, const char* b_) {

 return ::strcmp(a_, b_) == 0 ? 1: 0;

}

char* labels[] = {"Q","Q","W","W","E","E","R","T","T","Y","Y"};

int main() {

 const unsigned count = sizeof(labels) / sizeof(labels[0]);

 ostream_iterator‹char*› iter(cout);

 copy(labels, labels + count, iter);

 cout ‹‹ endl;

 unique(labels, labels + count, str_equal);

 copy(labels, labels + count, iter);

 cout ‹‹ endl;

 return 0;

}

parsrtc1.cpp

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

 vector‹int› result(5);

 ostream_iterator‹int› iter(cout, " ");

 copy(v1.begin(), v1.end(), iter);

 cout ‹‹ endl;

 partial_sort_copy(v1.begin(), v1.end(), result.begin(), result.end());

 copy(result.begin(), result.end(), iter);

 cout ‹‹ endl;

 return 0;

}

equal1.cpp

#include ‹stl.h›

#include ‹iostream.h›

int main() {

 vector‹int› v1(10);

 for (int i = 0; i ‹ v1.size(); i++) v1[i] = i;

 vector‹int› v2(10);

 if (equal(v1.begin(), v1.end(), v2.begin())) cout ‹‹ "v1 is equal to v2" ‹‹ endl;

 else cout ‹‹ "v1 is not equal to v2" ‹‹ endl;

 copy(v1.begin(), v1.end(), v2.begin());

 if (equal(v1.begin(), v1.end(), v2.begin())) cout ‹‹ "v1 is equal to v2" ‹‹ endl;

 else cout ‹‹ "v1 is not equal to v2" ‹‹ endl;

 return 0;

}

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;

1 ... 20 21 22 23 24 ... 30 ВПЕРЕД
Перейти на страницу:

Откройте для себя мир чтения на siteknig.com - месте, где каждая книга оживает прямо в браузере. Здесь вас уже ждёт произведение Александр Степанов - РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL), относящееся к жанру Программирование. Никаких регистраций, никаких преград - только вы и история, доступная в полном формате. Наш литературный портал создан для тех, кто любит комфорт: хотите читать с телефона - пожалуйста; предпочитаете ноутбук - идеально! Все книги открываются моментально и представлены полностью, без сокращений и скрытых страниц. Каталог жанров поможет вам быстро найти что-то по настроению: увлекательный роман, динамичное фэнтези, глубокую классику или лёгкое чтение перед сном. Мы ежедневно расширяем библиотеку, добавляя новые произведения, чтобы вам всегда было что открыть "на потом". Сегодня на siteknig.com доступно более 200000 книг - и каждая готова стать вашей новой любимой. Просто выбирайте, открывайте и наслаждайтесь чтением там, где вам удобно.

Комментарии (0)