char* names[] = {"Brett", "Graham", "Jack", "Mike", "Todd"};
int main() {
const unsigned namesCt = sizeof(names)/sizeof(names[0]);
cout ‹‹ *max_element(names, names + namesCt, str_compare) ‹‹ endl;
return 0;
}
#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"};
int main() {
const unsigned namesCt = sizeof(names)/sizeof(names[0]);
cout ‹‹ *min_element(names, names + namesCt, str_compare) ‹‹ endl;
return 0;
}
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(5);
iota(v1.begin(), v1.end(), 1);
vector‹int› v2(v1.size());
partial_sum(v1.begin(), v1.end(), v2.begin(), times‹int›());
ostream_iterator‹int› iter(cout, " ");
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
copy(v2.begin(), v2.end(), iter);
cout ‹‹ endl;
return 0;
}
#include ‹iostream.h›
#include ‹stl.h›
int main() {
char buffer[100];
int i = 0;
cin.unsetf(ios::skipws); // Disable white-space skipping.
cout ‹‹ "Please enter a string: ";
istream_iterator‹char, ptrdiff_t› s(cin);
while (*s!= 'n') buffer[i++] = *s++;
buffer[i] = ' '; // Null terminate buffer.
cout ‹‹ "read " ‹‹ buffer ‹‹ endl;
return 0;
}
#include ‹stl.h›
#include ‹iostream.h›
bool odd(int a_) {
return a_ % 2;
}
int numbers[6] = {2, 4, 8, 15, 32, 64};
int main() {
int* location = find_if(numbers, numbers + 6, odd);
if (location != numbers + 6)
cout ‹‹ "Value " ‹‹ *location ‹‹ " at offset " ‹‹ (location - numbers) ‹‹ " is odd" ‹‹ endl;
return 0;
}
#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());
v.push_back(7);
push_heap(v.begin(), v.end());
sort_heap(v.begin(), v.end());
ostream_iterator‹int› iter(cout, " ");
copy(v.begin(), v.end(), iter);
cout ‹‹ endl;
return 0;
}
#include ‹stl.h›
#include ‹iostream.h›
#include ‹string.h›
bool string_less(const char* a_, const char* b_) {
return ::strcmp(a_, b_) ‹ 0 ? 1 : 0;
}
char* letters[6] = {"bb", "aa", "ll", "dd", "qq", "cc"};
int main() {
stable_sort(letters, letters + 6, string_less);
for (int i = 0; i ‹ 6; i++) cout ‹‹ letters[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
#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++) {
next_permutation(v1.begin(), v1.end());
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
}
return 0;
}
#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());
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
}
return 0;
}
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(10);
iota(v1.begin(), v1.end(), 0);
ostream_iterator‹int› iter(cout, " ");
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
for (int i = 0; i ‹ 3; i++) {
random_shuffle(v1.begin(), v1.end());
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
}
return 0;
}
#include ‹iostream.h›
#include ‹stl.h›
int sum(int x_, int y_) {
return x_ + y_;
}
int input1[4] = {7, 2, 3, 5};
int input2[4] = {1, 5, 5, 8};
int main() {
int output[4];
transform(input1, input1 + 4, input2, output, pointer_to_binary_function‹int, int, int›(sum));
for (int i = 0; i ‹ 4; i++) cout ‹‹ output[i] ‹‹ endl;
return 0;
}
#include ‹iostream.h›
#include ‹stl.h›
void print (const vector‹const char*›& v_) {
vector‹const char*›::const_iterator i;
for (i = v_.begin(); i != v_.end(); i++) cout ‹‹ *i ‹‹ endl;
}
int main() {
vector‹const char*› v; // Vector of character strings.
v.push_back((char*) "zippy");
v.push_back((char*) "motorboy");
print (v);
return 0;
}
#include ‹stl.h›
#include ‹iostream.h›
int main() {
vector‹int› v1(10);
iota(v1.begin(), v1.end(), 0);
vector‹int› v2(v1.size());
partial_sum(v1.begin(), v1.end(), v2.begin());
ostream_iterator ‹int› iter(cout, " ");
copy(v1.begin(), v1.end(), iter);
cout ‹‹ endl;
copy(v2.begin(), v2.end(), iter);
cout ‹‹ endl;
return 0;
}
#include ‹stl.h›
#include ‹iostream.h›
bool odd(int a_) {
return a_ % 2;
}
int main() {
vector‹int› v1(10);
for (int i = 0; i ‹ v1.size(); i++) {
v1[i] = i % 5;
cout ‹‹ v1[i] ‹‹ ' ';
}
cout ‹‹ endl;
replace_if(v1.begin(), v1.end(), odd, 42);
for (i = 0; i ‹ v1.size(); i++) cout ‹‹ v1[i] ‹‹ ' ';
cout ‹‹ endl;
return 0;
}
#include ‹iostream.h›
#include ‹stl.h›
int array[] = {3, 6, 1, 2, 3, 2, 6, 7, 9};
int main() {
typedef multiset‹int, less‹int› › mset;
mset s(array, array + 9);
pair‹mset::const_iterator, mset::const_iterator› p = s.equal_range(3);
cout ‹‹ "lower bound = " ‹‹ *(p.first) ‹‹ endl;
cout ‹‹ "upper bound = " ‹‹ *(p.second) ‹‹ endl;
return 0;
}
#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*›::reverse_iterator i;
for (i = v.rbegin(); i!= v.rend(); i++) cout ‹‹ *i ‹‹ endl; // Display item.
return 0;
}
#include ‹iostream.h›
#include ‹stl.h›
int array1[] = {1, 16};
int array2[] = {4, 9};
int main() {
list‹int› l1(array1, array1 + 2);
list‹int› l2(array2, array2 + 2);
list‹int›::iterator i = l1.begin();
i++;
l1.splice(i, l2, l2.begin(), l2.end());
i = l1.begin();
while (i != l1.end()) cout ‹‹ *i++ ‹‹ endl;
return 0;
}
#include ‹iostream.h›
#include ‹stl.h›
int main() {
set‹int, less‹int› › s;
cout ‹‹ "count(42) = " ‹‹ s.count(42) ‹‹ endl;
s.insert(42);
cout ‹‹ "count(42) = " ‹‹ s.count(42) ‹‹ endl;
s.insert(42);
cout ‹‹ "count(42) = " ‹‹ s.count(42) ‹‹ endl;
int count = s.erase(42);
cout ‹‹ count ‹‹ " elements erased" ‹‹ endl;
return 0;
}
#include ‹iostream.h›
#include ‹stl.h›
int array1[] = {9, 16, 36};
int array2[] = {1, 4};
int main() {
list‹int› l1(array1, array1 + 3);
list‹int› l2(array2, array2 + 2);
list‹int›::iterator i1 = l1.begin();
l1.splice(i1, l2);
list‹int›::iterator i2 = l1.begin();
while (i2!= l1.end()) cout ‹‹ *i2++ ‹‹ endl;
return 0;
}
#include ‹iostream.h›
#include ‹stl.h›
int main() {
list‹int› years;
years.push_back(1962);
years.push_back(1992);
years.push_back(2001);
years.push_back(1999);
sort(years.begin(), years.end()); // Causes linker error.
list‹int›::iterator i;
for (i = years.begin(); i != years.end(); i++) cout ‹‹ *i ‹‹ endl;
return 0;
}
#include ‹stl.h›
#include ‹iostream.h›
int numbers[10] = {0, 0, 1, 1, 2, 2, 2, 2, 3, 3};