РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)
Шрифт:
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;
Поделиться с друзьями: