Александр Степанов - РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)
int numbers[10] = {0, 0, 1, 1, 2, 2, 2, 2, 3, 3};
int main() {
pair ‹int*, int*› range;
range = equal_range(numbers, numbers + 10, 2);
cout ‹‹ "2 can be inserted from before index " ‹‹ (range.first - numbers)
‹‹ " to before index " ‹‹ (range.second - numbers) ‹‹ endl;
return 0;
}
advance.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {
typedef vector‹int› IntVector;
IntVector v(10);
for (int i = 0; i ‹ v.size(); i++) v[i] = i;
IntVector::iterator location = v.begin();
cout ‹‹ "At Beginning: " ‹‹ *location ‹‹ endl;
advance(location, 5);
cout ‹‹ "At Beginning + 5: " ‹‹ *location ‹‹ endl;
return 0;
}
replace1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(10);
for (int i = 0; i ‹ v1.size(); i++) v1[i] = i % 5;
ostream_iterator‹int› iter(cout, " ");
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
replace(v1.begin(), v1.end(), 2, 42);
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
return 0;
}
alg3.cpp
#include ‹iostream.h›
#include ‹stl.h›
int main() {
vector‹int› i;
i.push_back(1);
i.push_back(4);
i.push_back(2);
i.push_back(8);
i.push_back(2);
i.push_back(2);
int n = 0; // Must be initialized, as count increments n.
count(i.begin(), i.end(), 2, n);
cout ‹‹ "Count of 2s = " ‹‹ n ‹‹ endl;
return 0;
}
func2.cpp
#include ‹iostream.h›
#include ‹stl.h›
bool bigger_than(int x_, int y_) {
return x_ › y_;
}
int main() {
vector‹int›v;
v.push_back(4);
v.push_back(1);
v.push_back(5);
sort(v.begin(), v.end(), bigger_than);
vector‹int›::iterator i;
for (i = v.begin(); i != v.end(); i++) cout ‹‹ *i ‹‹ endl;
return 0;
}
unegate1.cpp
#include ‹iostream.h›
#include ‹stl.h›
struct odd: public unary_function‹int, bool› {
odd() {}
bool operator() (int n_) const {return (n_ % 2) - 1;}
};
int array[3] = {1, 2, 3};
int main() {
int* p = find_if(array, array + 3, unary_negate‹odd›(odd()));
if (p != array + 3)
cout ‹‹ *p ‹‹ endl;
return 0;
}
alg4.cpp
#include ‹iostream.h›
#include ‹stl.h›
int main() {
vector‹int› years;
years.push_back(1962);
years.push_back(1992);
years.push_back(2001);
years.push_back(1999);
sort(years.begin(), years.end());
vector‹int›::iterator i;
for (i = years.begin(); i!= years.end(); i++)
cout ‹‹ *i ‹‹ endl;
return 0;
}
countif1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int odd(int a_) {
return a_ % 2;
}
int main() {
vector‹int› numbers(100);
for (int i = 0; i ‹ 100; i++) numbers[i] = i % 3;
int elements = 0;
count_if(numbers.begin(), numbers.end(), odd, elements);
cout ‹‹ "Found " ‹‹ elements ‹‹ " odd elements." ‹‹ endl;
return 0;
}
lwrbnd1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(20);
for (int i = 0; i ‹ v1.size(); i++) {
v1[i] = i/4;
cout ‹‹ v1[i] ‹‹ ' ';
}
int* location = lower_bound(v1.begin(), v1.end(), 3);
cout ‹‹ "n3 can be inserted at index: " ‹‹ (location - v1.begin()) ‹‹ endl;
return 0;
}
lexcmp1.cpp
#include ‹stl.h›
#include ‹iostream.h›
const unsigned size = 6;
char n1[size] = "shoe";
char n2[size] = "shine";
int main() {
bool before = lexicographical_compare(n1, n1 + size, n2, n2 + size);
if (before) cout ‹‹ n1 ‹‹ " is before " ‹‹ n2 ‹‹ endl;
else cout ‹‹ n2 ‹‹ " is before " ‹‹ n1 ‹‹ endl;
return 0;
}
copyb.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(10);
for (int i = 0; i ‹ v1.size(); i++) v1[i] = i;
vector‹int› v2(v1.size());
copy_backward(v1.begin(), v1.end(), v2.end());
ostream_iterator‹int› iter(cout, " ");
copy(v2.begin(), v2.end(), iter);
cout ‹‹ endl;
return 0;
}
ptrbinf2.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, ptr_fun(sum));
for (int i = 0; i ‹ 4; i++) cout ‹‹ output[i] ‹‹ endl;
return 0;
}
copyb0.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers[5] = {1, 2, 3, 4, 5};
int main() {
int result[5];
copy_backward(numbers, numbers + 5, result + 5);
for (int i = 0; i ‹ 5; i++) cout ‹‹ numbers[i] ‹‹ ' ';
cout ‹‹ endl;
for (i = 0; i ‹ 5; i++) cout ‹‹ result[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
binsert1.cpp
#include ‹iostream.h›
#include ‹stl.h›
char* array[] = {"laurie", "jennifer", "leisa"};
int main() {
vector‹char*› names;
copy(array, array + 3, back_insert_iterator‹vector‹char*› ›(names));
vector‹char*›::iterator i;
for (i = names.begin(); i!= names.end(); i++) cout ‹‹ *i ‹‹ endl;
return 0;
}
unegate2.cpp
#include ‹iostream.h›
#include ‹stl.h›
struct odd: public unary_function‹int, bool› {
odd() {}
bool operator() (int n_) const {return (n_ % 2) - 1;}
};
int array[3] = {1, 2, 3};
int main() {
int* p = find_if(array, array + 3, not1 (odd()));
if (p != array + 3) cout ‹‹ *p ‹‹ endl;
return 0;
}
revcopy1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers[6] = {0, 1, 2, 3, 4, 5};
int main() {
int result[6];
reverse_copy(numbers, numbers + 6, result);
for (int i = 0; i ‹ 6; i++) cout ‹‹ numbers[i] ‹‹ ' ';
cout ‹‹ endl;
for (i = 0; i ‹ 6; i++) cout ‹‹ result[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
finsert1.cpp
#include ‹iostream.h›
#include ‹stl.h›
char* array[] = {"laurie", "jennifer", "leisa"};
int main() {
deque‹char*› names;
copy(array, array + 3, front_insert_iterator‹deque ‹char*› ›(names));
deque‹char*›::iterator i;
for (i = names.begin(); i!= names.end(); i++) cout ‹‹ *i ‹‹ endl;
return 0;
}
remcpif1.cpp
#include ‹stl.h›
#include ‹iostream.h›
bool odd(int a_) {
return a_ % 2;
}
int numbers[6] = {1, 2, 3, 1, 2, 3};
int result[6] = {0, 0, 0, 0, 0, 0};
int main() {
remove_copy_if(numbers, numbers + 6, result, odd);
for (int i = 0; i ‹ 6; i++) cout ‹‹ result[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
inplmrg1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers[6] = {1, 10, 42, 3, 16, 32};
int main() {
for (int i = 0; i ‹ 6; i++) cout ‹‹ numbers[i] ‹‹ ' ';
cout ‹‹ endl;
inplace_merge(numbers, numbers + 3, numbers + 6);
for (i = 0; i ‹ 6; i++) cout ‹‹ numbers[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
list4.cpp
#include ‹iostream.h›
#include ‹stl.h›
int array1[] = {1, 3, 6, 7};
int array2[] = {2, 4};
int main() {
list‹int› l1(array1, array1 + 4);
list‹int› l2(array2, array2 + 2);
l1.merge(l2);
for (list‹int›::iterator i = l1.begin(); i != l1.end(); i++) cout ‹‹ *i;
cout ‹‹ endl;
return 0;
}
revbit1.cpp
#include ‹iostream.h›
#include ‹stl.h›
int array[] = {1, 5, 2, 3};
int main() {
list‹int› v(array, array + 4);
reverse_bidirectional_iterator‹list‹int›::iterator, int, list‹int›::reference, list‹int›::difference_type› r(v.end());
while (r != v.begin())
cout ‹‹ *r++ ‹‹ endl;
return 0;
}
copy3.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(10);
for (int i = 0; i ‹ v1.size(); i++) v1[i] = i;
vector‹int› v2(10);
copy(v1.begin(), v1.end(), v2.begin());
ostream_iterator‹int› iter(cout, " ");
copy(v2.begin(), v2.end(), iter);