Александр Степанов - РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)
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
#include ‹iostream.h›
#include ‹stl.h›
int main() {
priority_queue‹deque‹int›, less‹int› › q;
q.push(42);
q.push(101);
q.push(69);
while (!q.empty()) {
cout ‹‹ q.top() ‹‹ endl;
q.pop();
}
return 0;
}
genern1.cpp
#include ‹stl.h›
#include ‹iostream.h›
#include ‹stdlib.h›
int main() {
vector‹int› v1(10);
generate_n(v1.begin(), v1.size(), rand);
for (int i = 0; i ‹ 10; i++) cout ‹‹ v1[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
rotate0.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers[6] = {0, 1, 2, 3, 4, 5};
int main() {
rotate(numbers, numbers + 3, numbers + 6);
for (int i = 0; i ‹ 6; i++) cout ‹‹ numbers[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
foreach0.cpp
#include ‹stl.h›
#include ‹iostream.h›
void print(int a_) {
cout ‹‹ a_ ‹‹ ' ';
}
int numbers[10] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55};
int main() {
for_each(numbers, numbers + 10, print);
cout ‹‹ endl;
return 0;
}
alg2.cpp
#include ‹iostream.h›
#include ‹stl.h›
int i[] = {1, 4, 2, 8, 2, 2};
int main() {
int n = 0; // Must be initialized, as count increments n.
count(i, i + 6, 2, n);
cout ‹‹ "Count of 2s = " ‹‹ n ‹‹ endl;
return 0;
}
gener1.cpp
#include ‹stl.h›
#include ‹iostream.h›
#include ‹stdlib.h›
int main() {
int numbers[10];
generate(numbers, numbers + 10, rand);
for (int i = 0; i ‹ 10; i++) cout ‹‹ numbers[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
replace0.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers[6] = {0, 1, 2, 0, 1, 2};
int main() {
replace(numbers, numbers + 6, 2, 42);
for (int i = 0; i ‹ 6; i++) cout ‹‹ numbers[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
rndshuf0.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers[6] = {1, 2, 3, 4, 5, 6};
int main() {
random_shuffle(numbers, numbers + 6);
for (int i = 0; i ‹ 6; i++) cout ‹‹ numbers[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
bind1st2.cpp
#include ‹iostream.h›
#include ‹stl.h›
int array[3] = {1, 2, 3};
int main() {
int* p = remove_if(array, array + 3, bind1st(less‹int›(), 2));
for (int* i = array; i != p; i++) cout ‹‹ *i ‹‹ endl;
return 0;
}
unique1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers[8] = {0, 1, 1, 2, 2, 2, 3, 4};
int main() {
unique(numbers, numbers + 8);
for (int i = 0; i ‹ 8; i ++) cout ‹‹ numbers[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
bind2nd2.cpp
#include ‹iostream.h›
#include ‹stl.h›
int array[3] = {1, 2, 3};
int main() {
replace_if(array, array + 3, bind2nd(greater‹int (), 2), 4);
for (int i = 0; i ‹ 3; i++) cout ‹‹ array[i] ‹‹ endl;
return 0;
}
vec5.cpp
#include ‹iostream.h›
#include ‹stl.h›
int array[] = {1, 4, 9, 16};
int main() {
vector‹int› v(array, array + 4);
for (int i = 0; i ‹ v.size(); i++) cout ‹‹ "v[" ‹‹ i ‹‹ "] = " ‹‹ v[i] ‹‹ endl;
return 0;
}
iterswp0.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers[6] = {0, 1, 2, 3, 4, 5};
int main() {
iter_swap(numbers, numbers + 3);
for (int i = 0; i ‹ 6; i++) cout ‹‹ numbers[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
remove1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers[6] = {1, 2, 3, 1, 2, 3};
int main() {
remove(numbers, numbers + 6, 1);
for (int i = 0; i ‹ 6; i++) cout ‹‹ numbers[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
stblsrt1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int array[6] = {1, 50, -10, 11, 42, 19};
int main() {
stable_sort(array, array + 6);
for (int i = 0; i ‹ 6; i++) cout ‹‹ array[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
reverse1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers[6] = {0, 1, 2, 3, 4, 5};
int main() {
reverse(numbers, numbers + 6);
for (int i = 0; i ‹ 6; i++) cout ‹‹ numbers[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
logicnot.cpp
#include ‹iostream.h›
#include ‹stl.h›
bool input[7] = {1, 0, 0, 1, 1, 1, 1};
int main() {
int n = 0;
count_if(input, input + 7, logical_not‹bool›(), n);
cout ‹‹ "count = " ‹‹ n ‹‹ endl;
return 0;
}
bnegate2.cpp
#include ‹iostream.h›
#include ‹stl.h›
int array[4] = {4, 9, 7, 1};
int main() {
sort(array, array + 4, not2(greater‹int›()));
for (int i = 0; i ‹ 4; i++) cout ‹‹ array[i] ‹‹ endl;
return 0;
}
queue1.cpp
#include ‹iostream.h›
#include ‹stl.h›
int main() {
queue‹list‹int› › q;
q.push(42);
q.push(101);
q.push(69);
while (!q.empty()) {
cout ‹‹ q.front() ‹‹ endl;
q.pop();