mset3.cpp
#include ‹iostream.h›
#include ‹stl.h›
int array[] = {3, 6, 1, 2, 3, 2, 6, 7, 9};
int main() {
multiset‹int, less‹int› › s(array, array + 9);
multiset‹int, less‹int› ›::iterator i;
// Return location of first element that is not less than 3
i = s.lower_bound(3);
cout ‹‹ 'lower bound = ' ‹‹ *i ‹‹ endl;
// Return location of first element that is greater than 3
i = s.upper_bound(3);
cout ‹‹ 'upper bound = ' ‹‹ *i ‹‹ endl;
return 0;
}
binsrch2.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* labels[] = {'aa', 'dd', 'ff', 'jj', 'ss', 'zz'};
int main() {
const unsigned count = sizeof(labels) / sizeof(labels[0]);
if (binary_search(labels, labels + count, 'ff', str_compare)) cout ‹‹ 'ff is in labels.' ‹‹ endl;
else cout ‹‹ 'ff is not in labels.' ‹‹ endl;
return 0;
}
nthelem2.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() % 10;
ostream_iterator‹int› iter(cout, ' ');
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
nth_element(v1.begin(), v1.begin() + v1.size() / 2, v1.end(), greater‹int›());
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
return 0;
}
setintr1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(10);
iota(v1.begin(), v1.end(), 0);
vector‹int› v2(10);
iota(v2.begin(), v2.end(), 7);
ostream_iterator ‹int› iter(cout, ' ');
cout ‹‹ 'v1: ';
copy(v1.begin(), v1.end(), iter);
cout ‹‹ '
v2: ';
copy(v2.begin(), v2.end(), iter);
cout ‹‹ endl;
set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), iter);
cout ‹‹ endl;
return 0;
}
setdiff1.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(10);
iota(v1.begin(), v1.end(), 0);
vector‹int› v2(10);
iota(v2.begin(), v2.end(), 7);
ostream_iterator ‹int› iter(cout, ' ');
cout ‹‹ 'v1: ';