Александр Степанов - РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)
int main() {
vector‹int› numbers(100);
for (int i = 0; i ‹ 100; i++) numbers[i] = i % 3;
int elements = 0;
count(numbers.begin(), numbers.end(), 2, elements);
cout ‹‹ "Found " ‹‹ elements ‹‹ " 2's." ‹‹ endl;
return 0;
}
uniqcpy1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers[8] = {0, 1, 1, 2, 2, 2, 3, 4};
int result[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int main() {
unique_copy(numbers, numbers + 8, result);
for (int i = 0; i ‹ 8; i++) cout ‹‹ result[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
minus.cpp
#include ‹iostream.h›
#include ‹stl.h›
int input1[4] = {1, 5, 7, 8};
int input2[4] = {1, 4, 8, 3};
int main() {
int output[4];
transform(input1, input1 + 4, input2, output, minus‹int›());
for (int i = 0; i ‹ 4; i++) cout ‹‹ output[i] ‹‹ endl;
return 0;
}
replcpy1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers[6] = {0, 1, 2, 0, 1, 2};
int result[6] = {0, 0, 0, 0, 0, 0};
int main() {
replace_copy(numbers, numbers + 6, result, 2, 42);
for (int i = 0; i ‹ 6; i++) cout ‹‹ result[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
swprnge1.cpp
#include ‹stl.h›
#include ‹iostream.h›
#include ‹string.h›
int main() {
char* word1 = "World";
char* word2 = "Hello";
cout ‹‹ word1 ‹‹ " " ‹‹ word2 ‹‹ endl;
swap_ranges(word1, word1 + ::strlen(word1), word2);
cout ‹‹ word1 ‹‹ " " ‹‹ word2 ‹‹ endl;
return 0;
}
vec8.cpp
#include ‹iostream.h›
#include ‹stl.h›
int main() {
vector‹int› v;
cout ‹‹ "capacity = " ‹‹ v.capacity() ‹‹ endl;
v.push_back(42);
cout ‹‹ "capacity = " ‹‹ v.capacity() ‹‹ endl;
v.reserve (5000);
cout ‹‹ "capacity = " ‹‹ v.capacity() ‹‹ endl;
return 0;
}
plus.cpp
#include ‹iostream.h›
#include ‹stl.h›
int input1[4] = {1, 6, 11, 8};
int input2[4] = {1, 5, 2, 3};
int main() {
int total = inner_product(input1, input1 + 4, input2, 0, plus‹int›(), times‹int›());
cout ‹‹ "total = " ‹‹ total ‹‹ endl;
return 0;
}
remcopy1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers[6] = {1, 2, 3, 1, 2, 3};
int result[6] = {0, 0, 0, 0, 0, 0};
int main() {
remove_copy(numbers, numbers + 6, result, 2);
for (int i = 0; i ‹ 6; i++) cout ‹‹ result[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
error2.cpp
#include ‹stl.h›
// Compile this code with the symbol OS_USE_EXCEPTIONS defined.
int main() {
vector‹int› v;
try {
v.pop_back(); // Generates an exception.
} catch (const char* str) {
cout ‹‹ "Caught exception " ‹‹ str ‹‹ endl;
}
return 0;
}
iterswp1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(6);
iota(v1.begin(), v1.end(), 0);
iter_swap(v1.begin(), v1.begin() + 3);
ostream_iterator‹int› iter(cout, " ");
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
return 0;
}
remif1.cpp
#include ‹stl.h›
#include ‹iostream.h›
bool odd(int a_) {
return a_ % 2;
}
int numbers[6] = {0, 0, 1, 1, 2, 2};
int main() {
remove_if(numbers, numbers + 6, odd);
for (int i = 0; i ‹ 6; i++)
cout ‹‹ numbers[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
foreach1.cpp
#include ‹stl.h›
#include ‹iostream.h›
void print_sqr(int a_) {
cout ‹‹ a_ * a_ ‹‹ " ";
}
int main() {
vector‹int› v1(10);
for (int i = 0; i ‹ v1.size(); i++) v1[i] = i;
for_each(v1.begin(), v1.end(), print_sqr);
cout ‹‹ endl;
return 0;
}
parsrtc0.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers[6] = {5, 2, 4, 3, 1, 6};
int main() {
int result[3];
partial_sort_copy(numbers, numbers + 6, result, result + 3);
for (int i = 0; i ‹ 3; i++) cout ‹‹ result[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
pqueue2.cpp
#include ‹iostream.h›
#include ‹stl.h›
int main() {
priority_queue‹deque‹char*›, greater_s› q;
q.push((char*) "cat");
q.push((char*) "dog");
q.push((char*) "ape");
while (!q.empty()) {
cout ‹‹ q.top() ‹‹ endl;
q.pop();
}
return 0;
}
binsrch1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {
int vector[100];
for (int i = 0; i ‹ 100; i++) vector[i] = i;
if (binary_search(vector, vector + 100, 42)) cout ‹‹ "found 42" ‹‹ endl;
else cout ‹‹ "did not find 42" ‹‹ endl;
return 0;
}
ptrunf2.cpp
#include ‹iostream.h›
#include ‹stl.h›
bool even(int n_) {
return (n_ % 2) == 0;
}
int array[3] = {1, 2, 3};
int main() {
int* p = find_if(array, array + 3, ptr_fun(even));
if (p != array + 3) cout ‹‹ *p ‹‹ " is even" ‹‹ endl;
return 0;
}
rotcopy0.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers[6] = {0, 1, 2, 3, 4, 5};
int main() {
int result[6];
rotate_copy(numbers, numbers + 3, numbers + 6, result);
for (int i = 0; i ‹ 6; i++) cout ‹‹ result[i] ‹‹ ' ';
cout ‹‹ endl;
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);