Шрифт:
return (a_ * a_ == b_);
}
int main {
vector‹int› v1(10);
vector‹int› v2(10);
for (int i = 0; i ‹ v1.size; i++) {
v1[i] = i;
v2[i] = i * i;
}
if (equal(v1.begin, v1.end, v2.begin, values_squared))
cout ‹‹ "v2[i] == v1[i] * v1[i]" ‹‹ endl;
else cout ‹‹ "v2[i] != v1[i] * v1[i]" ‹‹ endl;
return 0;
}
inplmrg2.cpp
#include ‹stl.h›
#include ‹iostream.h›
int main {
vector‹int› v1(10);
for (int i = 0; i ‹ v1.size; i++) v1[i] = (v1.size - i - 1) % 5;
ostream_iterator‹int› iter(cout, " ");
copy(v1.begin, v1.end, iter);
cout ‹‹ endl;
inplace_merge(v1.begin, v1.begin + 5, v1.end, greater‹int›);
copy(v1.begin, v1.end, iter);
cout ‹‹ endl;
return 0;
}
nthelem1.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);
copy(v1.begin, v1.end, iter);
cout ‹‹ endl;
return 0;
}
vec4.cpp
#include ‹iostream.h›
#include ‹stl.h›
int main {
vector‹int› v(4);
v[0] = 1;
v[1] = 4;
v[2] = 9;
v[3] = 16;
cout ‹‹ "front = " ‹‹ v.front ‹‹ endl;
cout ‹‹ "back = " ‹‹ v.back ‹‹ ", size = " ‹‹ v.size ‹‹ endl;
v.push_back(25);
cout ‹‹ "back = " ‹‹ v.back ‹‹ ", size = " ‹‹ v.size ‹‹ endl;
v.pop_back;
cout ‹‹ "back = " ‹‹ v.back ‹‹ ", size = " ‹‹ v.size ‹‹ endl;
return 0;
}
lwrbnd2.cpp
#include ‹stl.h›
#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: "
‹‹ (lower_bound(str, str + strCt, "d", char_str_less) - str) ‹‹ endl;
return 0;
}
pheap2.cpp
#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, greater‹int›);
v.push_back(7);
push_heap(v.begin, v.end, greater‹int›);
sort_heap(v.begin, v.end, greater‹int›);
ostream_iterator‹int› iter(cout, " ");
copy(v.begin, v.end, iter);