#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;
}