Александр Степанов - РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)
cout ‹‹ endl;
return 0;
}
vec4.cpp
#include ‹iostream.h›
#include ‹stl.h›
int main() {
vector‹int› v(4);
v[0] = 1;
v[1] = 4;
v[2] = 9;
v[3] = 16;
cout ‹‹ "front = " ‹‹ v.front() ‹‹ endl;
cout ‹‹ "back = " ‹‹ v.back() ‹‹ ", size = " ‹‹ v.size() ‹‹ endl;
v.push_back(25);
cout ‹‹ "back = " ‹‹ v.back() ‹‹ ", size = " ‹‹ v.size() ‹‹ endl;
v.pop_back();
cout ‹‹ "back = " ‹‹ v.back() ‹‹ ", size = " ‹‹ v.size() ‹‹ endl;
return 0;
}
lwrbnd2.cpp
#include ‹stl.h›
#include ‹iostream.h›
#include ‹string.h›
bool char_str_less(const char* a_, const char* b_) {
return ::strcmp(a_, b_) ‹ 0 ? 1 : 0;
}
char* str[] = {"a", "a", "b", "b", "q", "w", "z"};
int main() {
const unsigned strCt = sizeof(str)/sizeof(str[0]);
cout ‹‹ "d can be inserted at index: "
‹‹ (lower_bound(str, str + strCt, "d", char_str_less) - str) ‹‹ endl;
return 0;
}
pheap2.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v;
v.push_back(1);
v.push_back(20);
v.push_back(4);
make_heap(v.begin(), v.end(), greater‹int›());
v.push_back(7);
push_heap(v.begin(), v.end(), greater‹int›());
sort_heap(v.begin(), v.end(), greater‹int›());
ostream_iterator‹int› iter(cout, " ");
copy(v.begin(), v.end(), iter);
cout ‹‹ endl;
return 0;
}
insert2.cpp
#include ‹iostream.h›
#include ‹stl.h›
char* array1[] = {"laurie", "jennifer", "leisa"};
char* array2[] = {"amanda", "saskia", "carrie"};
int main() {
deque‹char*› names(array1, array1 + 3);
deque‹char*›::iterator i = names.begin() + 2;
copy(array2, array2 + 3, inserter(names, i));
deque‹char*›::iterator j;
for (j = names.begin(); j!= names.end(); j++) cout ‹‹ *j ‹‹ endl;
return 0;
}
uprbnd2.cpp
#include ‹stl.h›
#include ‹iostream.h›
#include ‹string.h›
bool char_str_less(const char* a_, const char* b_) {
return ::strcmp(a_, b_) ‹ 0 ? 1 : 0;
}
char* str[] = {"a", "a", "b", "b", "q", "w", "z"};
int main() {
const unsigned strCt = sizeof(str)/sizeof(str[0]);
cout ‹‹ "d can be inserted at index: "
‹‹ upper_bound(str, str + strCt, "d", char_str_less) - str ‹‹ endl;
return 0;
}
vec3.cpp
#include ‹iostream.h›
#include ‹stl.h›
int main() {
vector‹char› v1; // Empty vector of characters.
v1.push_back('h');
v1.push_back('i');
cout ‹‹ "v1 = " ‹‹ v1[0] ‹‹ v1[1] ‹‹ endl;
vector‹char› v2(v1);
v2[1] = 'o'; // Replace second character.
cout ‹‹ "v2 = " ‹‹ v2[0] ‹‹ v2[1] ‹‹ endl;
cout ‹‹ "(v1 == v2) = " ‹‹ (v1 == v2) ‹‹ endl;
cout ‹‹ "(v1 ‹ v2) = " ‹‹ (v1 ‹ v2) ‹‹ endl;
return 0;
}
iter4.cpp
#include ‹iostream.h›
#include ‹stl.h›
int main() {
vector‹int› v; // Empty vector of integers.
v.push_back(1);
v.push_back(2);
v.push_back(3);
// Position immediately after last item.
vector‹int›::iterator i = v.end();
// Move back one and then access.
cout ‹‹ "last element is " ‹‹ *--i ‹‹ endl;
i -= 2; // Jump back two items.
cout ‹‹ "first element is " ‹‹ *i ‹‹ endl;
return 0;
}
setdiff0.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_difference(v1, v1 + 3, v2, v2 + 4, result);
for (int i = 0; i ‹ 4; i++) cout ‹‹ result[i] ‹‹ ' ';
cout ‹‹ endl;
set_difference(v2, v2 + 4, v1, v1 + 2, result);
for (i = 0; i ‹ 4; i++) cout ‹‹ result[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
lexcmp2.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, greater‹char›());
if (before) cout ‹‹ n1 ‹‹ " is after " ‹‹ n2 ‹‹ endl;
else cout ‹‹ n2 ‹‹ " is after " ‹‹ n1 ‹‹ endl;
return 0;
}
adjdiff1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v(10);
for (int i = 0; i ‹ v.size(); i++) v[i] = i * i;
vector‹int› result(v.size());
adjacent_difference(v.begin(), v.end(), result.begin());
ostream_iterator‹int› iter(cout, " ");
copy(v.begin(), v.end(), iter);
cout ‹‹ endl;
copy(result.begin(), result.end(), iter);
cout ‹‹ endl;
return 0;
}
stblptn1.cpp
#include ‹stl.h›
#include ‹stdlib.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(10);
for (int i = 0; i ‹ v1.size(); i++) v1[i] = rand() % 20;
ostream_iterator‹int› iter(cout, " ");
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
stable_partition(v1.begin(), v1.end(), bind2nd(less‹int›(), 11));
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
return 0;
}
ptition1.cpp
#include ‹stl.h›
#include ‹stdlib.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(10);
for (int i = 0; i ‹ v1.size(); i++) v1[i] = rand() % 20;
ostream_iterator ‹int› iter(cout, " ");
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
partition(v1.begin(), v1.end(), bind2nd(less‹int›(), 11));
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
return 0;
}
vec1.cpp
#include ‹iostream.h›
#include ‹stl.h›
int main() {
vector‹int› v1; // Empty vector of integers.
cout ‹‹ "empty = " ‹‹ v1.empty() ‹‹ endl;
cout ‹‹ "size = " ‹‹ v1.size() ‹‹ endl;
cout ‹‹ "max_size = " ‹‹ v1.max_size() ‹‹ endl;
v1.push_back(42); // Add an integer to the vector.
cout ‹‹ "size = " ‹‹ v1.size() ‹‹ endl;
cout ‹‹ "v1[0] = " ‹‹ v1[0] ‹‹ endl;
return 0;
}
sort2.cpp
#include ‹stl.h›
#include ‹iostream.h›
int array[] = {1, 50, -10, 11, 42, 19};
int main() {
int count = sizeof(array) / sizeof(array[0]);
ostream_iterator ‹int› iter(cout, " ");
cout ‹‹ "before: ";
copy(array, array + count, iter);
cout ‹‹ "nafter: ";
sort(array, array + count, greater‹int›());
copy(array, array + count, iter);
cout ‹‹ endl;
return 0;
}
copy4.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {
typedef vector‹int› IVec;
vector‹int› v1(10);
for (int loc = 0; loc ‹ v1.size(); loc++) v1[loc] = loc;
vector‹int› v2;
insert_iterator‹IVec› i (v2, v2.begin());
copy(v1.begin(), v1.end(), i);
ostream_iterator‹int› outiter(cout, " ");
copy(v2.begin(), v2.end(), outIter);
cout ‹‹ endl;
return 0;
}
prevprm2.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(3);
iota(v1.begin(), v1.end(), 0);
ostream_iterator‹int› iter(cout, " ");
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
for (int i = 0; i ‹ 9; i++) {
prev_permutation(v1.begin(), v1.end(), greater‹int›());
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
}
return 0;
}
trnsfrm2.cpp
#include ‹stl.h›
#include ‹iostream.h›
#include ‹string.h›
char map_char(char a_, int b_) {
return char(a_ + b_);
}
int trans[] = {-4, 4, -6, -6, -10, 0, 10, -6, 6, 0, -1, -77};
char n[] = "Larry Mullen";
int main() {
const unsigned count = ::strlen(n);
ostream_iterator ‹char› iter(cout);
transform(n, n + count, trans, iter, map_char);
cout ‹‹ endl;
return 0;
}
iter1.cpp
#include ‹iostream.h›
#include ‹stl.h›
int main() {
vector‹const char*› v; // Vector of character strings.
v.push_back((char*) "zippy"); // First element.
v.push_back((char*) "motorboy"); // Second element.
vector‹const char*›::iterator i = v.begin(); // Position at end.
for (i = v.begin(); i != v.end(); i++) cout ‹‹ *i ‹‹ endl; // Display item.
return 0;
}
maxelem2.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;
}
char* names[] = {"Brett", "Graham", "Jack", "Mike", "Todd"};