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

ЖАНРЫ

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

Менг Ли

Шрифт:

 return 0;

}

mkheap0.cpp

#include ‹stl.h›

#include ‹iostream.h›

int numbers[6] = {5, 10, 4, 13, 11, 19};

int main {

 make_heap(numbers, numbers + 6);

 for (int i = 6; i ›= 1; i--) {

cout ‹‹ numbers[0] ‹‹ endl;

pop_heap(numbers, numbers + i);

 }

 return 0;

}

copy1.cpp

#include ‹stl.h›

#include ‹iostream.h›

#include ‹string.h›

char string[23] = "A string to be copied.";

int main {

 char result[23];

 copy(string, string + 23, result);

 cout ‹‹ " Src: " ‹‹ string ‹‹ "\nDest: " ‹‹ result ‹‹ endl;

 return 0;

}

find0.cpp

#include ‹stl.h›

#include ‹iostream.h›

int numbers[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64};

int main {

 int* location;

 location = find(numbers, numbers + 10, 25);

 cout ‹‹ "Found 25 at offset " ‹‹ (location - numbers) ‹‹ endl;

 return 0;

}

partsum0.cpp

#include ‹stl.h›

#include ‹iostream.h›

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

int main {

 int result[6];

 partial_sum(numbers, numbers + 6, result);

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

 cout ‹‹ endl;

 return 0;

}

bvec1.cpp

#include ‹iostream.h›

#include ‹stl.h›

int main {

 bit_vector b(3);

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

 cout ‹‹ endl;

 b[0] = b[2] = 1;

 for (i = 0; i ‹ b.size; i++) cout ‹‹ b[i];

 cout ‹‹ endl;

 return 0;

}

bind2nd1.cpp

#include ‹iostream.h›

#include ‹stl.h›

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

int main {

 replace_if(array, array + 3, binder2nd‹greater‹int› ›(greater‹int›, 2), 4);

 for (int i = 0; i ‹ 3; i++) cout ‹‹ array[i] ‹‹ endl;

 return 0;

}

bind1st1.cpp

#include ‹iostream.h›

#include ‹stl.h›

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

int main {

 int* p = remove_if(array, array + 3, binder1st‹less‹int› ›(less‹int›, 2));

 for (int* i = array; i != p; i++) cout ‹‹ *i ‹‹ endl;

 return 0;

}

reviter2.cpp

#include ‹iostream.h›

#include ‹stl.h›

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

int main {

 vector‹int› v(array, array + 4);

 vector‹int›::reverse_iterator r;

 for (r = v.rbegin; r != v.rend; r++) cout ‹‹ *r ‹‹ endl;

 return 0;

}

copy2.cpp

#include ‹stl.h›

#include ‹iostream.h›

int main {

 vector‹int› v(10);

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

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

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

 cout ‹‹ endl;

 return 0;

}

max2.cpp

#include ‹stl.h›

#include ‹iostream.h›

#include ‹string.h›

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

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

}

int main {

 cout ‹‹ max("shoe", "shine", str_compare) ‹‹ endl;

 return 0;

}

min2.cpp

#include ‹stl.h›

#include ‹iostream.h›

#include ‹string.h›

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

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

}

int main {

 cout ‹‹ min("shoe", "shine", str_compare) ‹‹ endl;

 return 0;

}

parsrt0.cpp

#include ‹stl.h›

#include ‹iostream.h›

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

int main {

 partial_sort(numbers, numbers + 3, numbers + 6);

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

 cout ‹‹ endl;

 return 0;

}

partsrt0.cpp

#include ‹stl.h›

#include ‹iostream.h›

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

int main {

 partial_sort(numbers, numbers + 3, numbers + 6);

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

 cout ‹‹ endl;

 return 0;

}

bnegate1.cpp

#include ‹iostream.h›

#include ‹stl.h›

int array[4] = {4, 9, 7, 1};

int main {

 sort(array, array + 4, binary_negate‹greater‹int› ›(greater‹int›));

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

 return 0;

}

nthelem0.cpp

#include ‹stl.h›

#include ‹iostream.h›

int numbers[6] = {5, 2, 4, 1, 0, 3};

int main {

 nth_element(numbers, numbers + 3, numbers + 6);

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

 cout ‹‹ endl;

 return 0;

}

revbit2.cpp

#include ‹iostream.h›

#include ‹stl.h›

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

int main {

 list‹int› v(array, array + 4);

 list‹int›::reverse_iterator r;

 for (r = v.rbegin; r != v.rend; r++) cout ‹‹ *r ‹‹ endl;

 return 0;

}

count0.cpp

#include ‹stl.h›

#include ‹iostream.h›

int numbers[10] = {1, 2, 4, 1, 2, 4, 1, 2, 4, 1};

int main {

 int result = 0;

 count(numbers, numbers + 10, 1, result);

 cout ‹‹ "Found " ‹‹ result ‹‹ " 1's." ‹‹ endl;

 return 0;

}

negate.cpp

#include ‹iostream.h›

#include ‹stl.h›

int input[3] = {1, 2, 3};

int main {

 int output[3];

 transform(input, input + 3, output, negate‹int›);

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

 return 0;

}

pqueue1.cpp

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