Александр Степанов - РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)
copy(v2.begin(), v2.end(), iter);
cout ‹‹ endl;
return 0;
}
merge0.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers1[5] = {1, 6, 13, 25, 101};
int numbers2[5] = {-5, 26, 36, 46, 99};
int main() {
int result[10];
merge(numbers1, numbers1 + 5, numbers2, numbers2 + 5, result);
for (int i = 0; i ‹ 10; i++) cout ‹‹ result[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
reviter1.cpp
#include ‹iostream.h›
#include ‹stl.h›
int array[] = {1, 5, 2, 3};
int main() {
vector‹int› v(array, array + 4);
stl_reverse_iterator‹vector‹int›::iterator, int, vector‹int›::reference, vector‹int›::difference_type› r (v.end());
while (r!= v.begin()) cout ‹‹ *r++ ‹‹ endl;
return 0;
}
find1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int years[] = {1942, 1952, 1962, 1972, 1982, 1992};
int main() {
const unsigned yearCount = sizeof(years) / sizeof(years[0]);
int* location = find(years, years + yearCount, 1972);
cout ‹‹ "Found 1972 at offset " ‹‹ (location - years) ‹‹ endl;
return 0;
}
trnsfrm1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int negate_int(int a_) {
return -a_;
}
int numbers[6] = {-5, -1, 0, 1, 6, 11};
int main() {
int result[6];
transform(numbers, numbers + 6, result, negate_int);
for (int i = 0; i ‹ 6; i++) cout ‹‹ result[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
binsert2.cpp
#include ‹iostream.h›
#include ‹stl.h›
char* array[] = {"laurie", "jennifer", "leisa"};
int main() {
vector‹char*› names;
copy(array, array + 3, back_inserter(names));
vector‹char*›::iterator i;
for (i = names.begin(); i!= names.end(); i++) cout ‹‹ *i ‹‹ endl;
return 0;
}
finsert2.cpp
#include ‹iostream.h›
#include ‹stl.h›
char* array[] = {"laurie", "jennifer", "leisa"};
int main() {
deque‹char*› names;
copy(array, array + 3, front_inserter(names));
deque‹char*›::iterator i;
for (i = names.begin(); i!= names.end(); i++) cout ‹‹ *i ‹‹ endl;
return 0;
}
mset2.cpp
#include ‹iostream.h›
#include ‹stl.h›
char* names[] = {"dave", "alf", "chas", "bob", "ed", "chas"};
int main() {
typedef multiset‹char*, less_s› mset;
mset s;
s.insert(names, names + 6);
for (mset::iterator i = s.begin(); i!= s.end(); i++) cout ‹‹ *i ‹‹ endl;
return 0;
}
ostmit.cpp
#include ‹iostream.h›
#include ‹stl.h›
int array[] = {1, 5, 2, 4};
int main() {
char* string = "hello";
ostream_iterator‹char› it1(cout);
copy(string, string + 5, it1);
cout ‹‹ endl;
ostream_iterator‹int› it2(cout);
copy(array, array + 4, it2);
cout ‹‹ endl;
return 0;
}
ptrunf1.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, pointer_to_unary_function‹int, bool›(even));
if (p != array + 3) cout ‹‹ *p ‹‹ " is even" ‹‹ endl;
return 0;
}
func1.cpp
#include ‹iostream.h›
#include ‹stl.h›
bool bigger(int i_) {
return i_ › 3;
}
int main() {
vector‹int› v;
v.push_back(4);
v.push_back(1);
v.push_back(5);
int n = 0;
count_if(v.begin(), v.end(), bigger, n);
cout ‹‹ "Number greater than 3 = " ‹‹ n ‹‹ endl;
return 0;
}
stblptn0.cpp
#include ‹stl.h›
#include ‹iostream.h›
bool less_10(int a_) {
return a_ ‹ 10 ? 1 : 0;
}
int numbers[6] = {10, 5, 11, 20, 6, -2};
int main() {
stable_partition(numbers, numbers + 6, less_10);
for (int i = 0; i ‹ 6; i++) cout ‹‹ numbers[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
setunon0.cpp
#include ‹stl.h›
#include ‹iostream.h›
int v1[3] = {13, 18, 23};
int v2[4] = {10, 13, 17, 23};
int result[7] = {0, 0, 0, 0, 0, 0, 0};
int main() {
set_union(v1, v1 + 3, v2, v2 + 4, result);
for (int i = 0; i ‹ 7; i++) cout ‹‹ result[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
mkheap1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers[6] = {5, 10, 4, 13, 11, 19};
int main() {
make_heap(numbers, numbers + 6, greater‹int›());
for (int i = 6; i ›= 1; i--) {
cout ‹‹ numbers[0] ‹‹ endl;
pop_heap(numbers, numbers + i, greater‹int›());
}
return 0;
}
setintr0.cpp
#include ‹stl.h›
#include ‹iostream.h›
int v1[3] = {13, 18, 23};
int v2[4] = {10, 13, 17, 23};
int result[4] = {0, 0, 0, 0};
int main() {
set_intersection(v1, v1 + 3, v2, v2 + 4, result);
for (int i = 0; i ‹ 4; i++) cout ‹‹ result[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
logicand.cpp
#include ‹iostream.h›
#include ‹stl.h›
bool input1[4] = {1, 1, 0, 1};
bool input2[4] = {0, 1, 0, 0};
int main() {
int output[4];
transform(input1, input1 + 4, input2, output, logical_and‹bool›());
for (int i = 0; i ‹ 4; i++) cout ‹‹ output[i] ‹‹ endl;
return 0;
}
logicor.cpp
#include ‹iostream.h›
#include ‹stl.h›
bool input1[4] = {1, 1, 0, 1};
bool input2[4] = {0, 1, 0, 0};
int main() {
int output[4];
transform(input1, input1 + 4, input2, output, logical_or‹bool›());
for (int i = 0; i ‹ 4; i++) cout ‹‹ output[i] ‹‹ endl;
return 0;
}
nequal.cpp
#include ‹iostream.h›
#include ‹stl.h›
int input1[4] = {1, 7, 2, 2};
int input2[4] = {1, 6, 2, 3};
int main() {
int output[4];
transform(input1, input1 + 4, input2, output, not_equal_to‹int›());
for (int i = 0; i ‹ 4; i++) cout ‹‹ output[i] ‹‹ endl;
return 0;
}
ptition0.cpp
#include ‹stl.h›
#include ‹iostream.h›
int less_10(int a_) {
return a_ ‹ 10 ? 1 : 0;
}
int numbers[6] = {6, 12, 3, 10, 1, 20};
int main() {
partition(numbers, numbers + 6, less_10);
for (int i = 0; i ‹ 6; i++) cout ‹‹ numbers[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
inrprod0.cpp
#include ‹stl.h›
#include ‹iostream.h›
#include ‹string.h›
int vector1[5] = {1, 2, 3, 4, 5};
int vector2[5] = {1, 2, 3, 4, 5};
int main() {
int result;
result = inner_product(vector1, vector1 + 5, vector2, 0);
cout ‹‹ "Inner product = " ‹‹ result ‹‹ endl;
return 0;
}
func3.cpp
#include ‹iostream.h›
#include ‹stl.h›
int main() {
vector‹int›v;
v.push_back(4);
v.push_back(1);
v.push_back(5);
sort(v.begin(), v.end(), greater‹int›());
vector‹int›::iterator i;
for (i = v.begin(); i != v.end(); i++) cout ‹‹ *i ‹‹ endl;
return 0;
}
modulus.cpp
#include ‹iostream.h›
#include ‹stl.h›
int input1[4] = {6, 8, 10, 2};
int input2[4] = {4, 2, 11, 3};
int main() {
int output[4];
transform(input1, input1 + 4, input2, output, modulus‹int›());
for (int i = 0; i ‹ 4; i++) cout ‹‹ output[i] ‹‹ endl;
return 0;
}
uprbnd1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {
int array[20];
for (int i = 0; i ‹ 20; i++) {
array[i] = i/4;
cout ‹‹ array[i] ‹‹ ' ';
}
cout ‹‹ "n3 can be inserted at index: "
‹‹ upper_bound(array, array + 20, 3) - array ‹‹ endl;
return 0;
}
equalto.cpp
#include ‹iostream.h›
#include ‹stl.h›
int input1[4] = {1, 7, 2, 2};
int input2[4] = {1, 6, 2, 3};
int main() {
int output[4];
transform(input1, input1 + 4, input2, output, equal_to‹int›());
for (int i = 0; i ‹ 4; i++) cout ‹‹ output[i] ‹‹ endl;
return 0;
}
count1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {