Чтение онлайн

ЖАНРЫ

РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)

Менг Ли

Шрифт:

 return 0;

}

pheap1.cpp

#include ‹stl.h›

#include ‹iostream.h›

int main {

 vector‹int› v;

 v.push_back(1);

 v.push_back(20);

 v.push_back(4);

 make_heap(v.begin, v.end);

 v.push_back(7);

 push_heap(v.begin, v.end);

 sort_heap(v.begin, v.end);

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

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

 cout ‹‹ endl;

 return 0;

}

stblsrt2.cpp

#include ‹stl.h›

#include ‹iostream.h›

#include ‹string.h›

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

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

}

char* letters[6] = {"bb", "aa", "ll", "dd", "qq", "cc"};

int main {

 stable_sort(letters, letters + 6, string_less);

 for (int i = 0; i ‹ 6; i++) cout ‹‹ letters[i] ‹‹ ' ';

 cout ‹‹ endl;

 return 0;

}

nextprm1.cpp

#include ‹stl.h›

#include ‹iostream.h›

int main {

 vector‹int› v1(3);

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

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

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

 cout ‹‹ endl;

 for (int i = 0; i ‹ 9; i++) {

next_permutation(v1.begin, v1.end);

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

cout ‹‹ endl;

 }

 return 0;

}

prevprm1.cpp

#include ‹stl.h›

#include ‹iostream.h›

int main {

 vector‹int› v1(3);

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

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

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

 cout ‹‹ endl;

 for (int i = 0; i ‹ 9; i++) {

prev_permutation(v1.begin, v1.end);

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

cout ‹‹ endl;

 }

 return 0;

}

rndshuf1.cpp

#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 ‹ 3; i++) {

random_shuffle(v1.begin, v1.end);

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

cout ‹‹ endl;

 }

 return 0;

}

ptrbinf1.cpp

#include ‹iostream.h›

#include ‹stl.h›

int sum(int x_, int y_) {

 return x_ + y_;

}

int input1[4] = {7, 2, 3, 5};

int input2[4] = {1, 5, 5, 8};

int main {

 int output[4];

 transform(input1, input1 + 4, input2, output, pointer_to_binary_function‹int, int, int›(sum));

 for (int i = 0; i ‹ 4; i++) cout ‹‹ output[i] ‹‹ endl;

 return 0;

}

iter2.cpp

#include ‹iostream.h›

#include ‹stl.h›

void print (const vector‹const char*›& v_) {

 vector‹const char*›::const_iterator i;

 for (i = v_.begin; i != v_.end; i++) cout ‹‹ *i ‹‹ endl;

}

int main {

 vector‹const char*› v; // Vector of character strings.

 v.push_back((char*) "zippy");

 v.push_back((char*) "motorboy");

 print (v);

 return 0;

}

partsum1.cpp

#include ‹stl.h›

#include ‹iostream.h›

int main {

 vector‹int› v1(10);

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

 vector‹int› v2(v1.size);

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

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

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

 cout ‹‹ endl;

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

 cout ‹‹ endl;

 return 0;

}

Поделиться с друзьями: