РУКОВОДСТВО ПО СТАНДАРТНОЙ БИБЛИОТЕКЕ ШАБЛОНОВ (STL)
Шрифт:
release(v.begin, v.end); // Delete heap-based objects.
return 0;
}
incl1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main {
vector‹int› v1(10);
vector‹int› v2(3);
for (int i = 0; i ‹ v1.size; i++) {
v1[i] = i;
}
if (includes(v1.begin, v1.end, v2.begin, v2.end)) cout ‹‹ "v1 includes v2" ‹‹ endl;
else cout ‹‹ "v1 does not include v2" ‹‹ endl;
for (i = 0; i ‹ v2.size; i++) v2[i] = i + 3;
if (includes(v1.begin, v1.end, v2.begin, v2.end)) cout ‹‹ "v1 includes v2" ‹‹ endl;
else cout ‹‹ "v1 does not include v2" ‹‹ endl;
return 0;
}
setintr2.cpp
#include ‹stl.h›
#include ‹iostream.h›
#include ‹string.h›
char* word1 = "ABCDEFGHIJKLMNO";
char* word2 = "LMNOPQRSTUVWXYZ";
int main {
ostream_iterator ‹char› iter(cout, " ");
cout ‹‹ "word1: ";
copy(word1, word1 + ::strlen(word1), iter);
cout ‹‹ "\nword2: ";
copy(word2, word2 + ::strlen(word2), iter);
cout ‹‹ endl;
set_intersection(word1, word1 + ::strlen(word1), word2, word2 + ::strlen(word2), iter, less‹char›);
cout ‹‹ endl;
return 0;
}
inrprod1.cpp
#include ‹stl.h›
#include ‹iostream.h›
#include ‹string.h›
int main {
vector‹int› v1(3);
vector‹int› v2(v1.size);
for (int i = 0; i ‹ v1.size; i++) {
v1[i] = i + 1;
v2[i] = v1.size - i;
}
ostream_iterator‹int› iter(cout, " ");
cout ‹‹ "Inner product(sum of products) of:\n\t";
copy(v1.begin, v1.end, iter);
cout ‹‹ "\n\t";
copy(v2.begin, v2.end, iter);
int result = inner_product(v1.begin, v1.end, v2.begin, 0);
cout ‹‹ "\nis: " ‹‹ result ‹‹ endl;
return 0;
}
merge1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main {
vector‹int› v1(5);
vector‹int› v2(v1.size);
iota(v1.begin, v1.end, 0);
iota(v2.begin, v2.end, 3);
vector‹int› result(v1.size + v2.size);
merge(v1.begin, v1.end, v2.begin, v2.end, result.begin);
ostream_iterator‹int› iter(cout, " ");
copy(v1.begin, v1.end, iter);
cout ‹‹ endl;
copy(v2.begin, v2.end, iter);
cout ‹‹ endl;
copy(result.begin, result.end, iter);
cout ‹‹ endl;
return 0;
}
bcompos2.cpp
#include ‹iostream.h›
#include ‹stl.h›
struct odd: public unary_function‹int, bool› {
odd {}
bool operator (int n_) const {return (n_ % 2) - 1;}
};
struct positive: public unary_function‹int, bool› {
positive {}
bool operator (int n_) const {return n_ ›= 0;}
};
int array[6] = {-2, -1, 0, 1, 2, 3};
int main {
int* p = find_if(array, array + 6, compose2(logical_and‹bool›, odd, positive));
if (p != array + 6) cout ‹‹ *p ‹‹ " is odd and positive" ‹‹ endl;
return 0;
}
error3.cpp
#include ‹stl.h›
// Compile this code without defining OS_USE_EXCEPTIONS.
void my_handler(int code_, const char* str_) {
cout ‹‹ "Caught " ‹‹ str_ ‹‹ "[code " ‹‹ code_ ‹‹ "]" ‹‹ endl;
}
int main {
os_handler_function_t old_h = os_set_error_handler(my_handler);
vector‹int› v;
v.pop_back; // Generates an empty object error.
cout ‹‹ "returned from pop_back" ‹‹ endl;
os_set_error_handler(old_h);
v.pop_back; // Generates an empty object error.
cout ‹‹ "successful termination" ‹‹ endl;
return 0;
}
incl0.cpp
#include ‹stl.h›
#include ‹iostream.h›
int numbers1[5] = {1, 2, 3, 4, 5};
int numbers2[5] = {1, 2, 4, 8, 16};
int numbers3[2] = {4, 8};
int main {
if (includes(numbers1, numbers1 + 5, numbers3, numbers3 + 2))
Поделиться с друзьями: