Шрифт:
#include ‹string.h›
char chars[] = "aabbccddggghhklllmqqqqssyyzz";
int main {
const unsigned count = sizeof(chars) - 1;
ostream_iterator‹char› iter(cout);
cout ‹‹ "Within the collection:\n\t";
copy(chars, chars + count, iter);
pair‹char*, char*› range;
range = equal_range(chars, chars + count, 'q', less‹char›);
cout ‹‹ "\nq can be inserted from before index " ‹‹ (range.first - chars) ‹‹ " to before index "
‹‹ (range.second - chars) ‹‹ endl;
return 0;
}
release1.cpp
#include ‹stl.h›
#include ‹iostream.h›
class X {
public:
X(int i_): i(i_) {}
~X {cout ‹‹ "Delete X(" ‹‹ i ‹‹ ")" ‹‹ endl;}
int i;
};
ostream& operator ‹‹ (ostream& stream_, const X& x_) {
return stream_ ‹‹ "X(" ‹‹ x_.i ‹‹ ")";
}
int main {
vector‹X*› v;
v.push_back(new X(2));
v.push_back(new X(1));
v.push_back(new X(4));
vector‹X*›::iterator i;
for (i = v.begin; i!= v.end; i++) cout ‹‹ *(*i) ‹‹ endl;
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);