STL的heap测试
int ai[9] = { 0,1,2,3,4,5,6,7,8 };
vector<int> vec(ai, ai + 9);
make_heap(vec.begin(),vec.end());
for (auto i : vec)
cout << i << " ";
cout << endl;
//8 7 6 3 4 5 2 1 0
vec.push_back(10);
make_heap(vec.begin(), vec.end());
for (auto i : vec)
cout << i << " ";
cout << endl;
//10 8 6 3 7 5 2 1 0 4
pop_heap(vec.begin(), vec.end());
cout << vec.back() << endl;
vec.pop_back();
for (auto i : vec)
cout << i << " ";
cout << endl;
//10
//8 7 6 3 4 5 2 1 0
sort_heap(vec.begin(), vec.end());
for (auto i : vec)
cout << i << " ";
cout << endl;
//0 1 2 3 4 5 6 7 8
sort_heap(vec.begin(), vec.end(), greater<int>());
for (auto i : vec)
cout << i << " ";
cout << endl;
//8 7 6 5 4 3 2 1 0
sort_heap(vec.begin(), vec.end(), less<int>());
for (auto i : vec)
cout << i << " ";
cout << endl;
//0 1 2 3 4 5 6 7 8
make_heap(ai, ai + 9);
for (int i = 0; i < 9; i++)
cout << ai[i] << " ";
cout << endl;
sort_heap(ai, ai + 9);
for (int i = 0; i < 9; i++)
cout << ai[i] << " ";
cout << endl;
//8 7 6 3 4 5 2 1 0
//经过sort的heap不是一个合法的heap,需重做一个
pop_heap(ai, ai + 9);
cout << "hello" << endl;
for (int i = 0; i < 9; i++)
cout << ai[i] << " ";
cout << endl;
//7 4 6 3 0 5 2 1 8
make_heap(ai, ai + 9);//经过sort的heap不是一个合法的heap,需重做一个
pop_heap(ai, ai + 9);
cout << ai[8] << endl;
//8
STL的priority_queue测试
int ai[9] = { 0,1,2,3,4,5,6,7,8 };
priority_queue<int> pq(ai,ai+9);
cout << "size: " << pq.size() << endl;
for (int i = 0; i < pq.size(); i++)
cout << pq.top() << " ";
cout << endl;
while (pq.size()) {
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
//size: 9
//8 8 8 8 8 8 8 8 8
//8 7 6 5 4 3 2 1 0
STL的slist测试,内部的结构有一个dummy node
自己环境#include报错,测试代码见STL源码剖析P191
数值算法
int ai[5] = { 1,2,3,4,5 };
vector<int> vec(ai, ai + 5);
vector<int>ans(5,0);
cout << accumulate(vec.begin(), vec.end(), 0) << endl;
//15
adjacent_difference(vec.begin(), vec.end(), ans.begin());
for (auto i : ans)
cout << i << " ";
cout << endl;
// 1 1 1 1 1
cout << inner_product(vec.begin(),vec.end(),vec.begin(),0) << endl;
// 55
ostream_iterator<int> oite(cout, " ");
partial_sum(vec.begin(), vec.end(), oite);
cout << endl;
//1 3 6 10 15
iota(vec.begin(), vec.end(), 3);
for (auto i : vec)
cout << i << " ";
//3 4 5 6 7
基本算法
int ai[9] = { 1,2,3,4,5,6,7,8,9 };
vector<int> vec1(ai, ai + 5);
vector<int> vec2(ai, ai + 9);
cout << equal(vec1.begin(), vec1.end(), vec2.begin()) << endl;
// 1
if ((mismatch(vec1.begin(), vec1.end(), vec2.begin(), vec2.end())).first == vec1.end())
cout << "vec1 结束" << endl;
// vec1 结束
cout << (*(mismatch(vec1.begin(), vec1.end(), vec2.begin(), vec2.end()).second)) << endl;
// 6
fill(vec1.begin(), vec1.end(), 9);
for_each(vec1.begin(), vec1.end(), function1<int>());
cout << endl;
// 9 9 9 9 9
fill_n(vec2.begin(), 7, 2);
for_each(vec2.begin(), vec2.end(), function1<int>());
cout << endl;
// 2 2 2 2 2 2 2 8 9
fill_n(inserter(vec2,vec2.begin()), 20, 1);
for_each(vec2.begin(), vec2.end(), function1<int>());
cout << endl;
// 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 8 9
string str1[] = { "Jamie","JJHou","Jason" };
string str2[] = { "Jamie","JJHou","Jason" };
cout << lexicographical_compare(str1, str1+2, str2, str2+2);
// 0
int ai[ ] = {0, 1,2,3,4,5,6,6,6,6,7,8};
vector<int> vec(ai, ai + sizeof(ai) / sizeof(int));
auto first = vec.begin(), last = vec.end();
cout << *adjacent_find(vec.begin(), vec.end()) << endl;
// 6
cout << count(first, last, 6) << endl;
// 4
cout << count_if(first, last, bind2nd(less<int>(), 7)) << endl;
//10
cout << *find(first, last, 2) << endl;
// 2
cout << *find_if(first, last, bind2nd(greater<int>(), 2)) << endl;
// 3
vector<int> iv2(ai + 6, ai + 8);
for_each(iv2.begin(), iv2.end(), function1<int>());
cout << endl;
// 6 6
cout << *(find_end(first, last, iv2.begin(), iv2.end())+3) << endl;
// 8
cout << *(find_first_of(first, last, iv2.begin(), iv2.end()) + 4) << endl;
// 7
generate(iv2.begin(), iv2.end(), even_by_two());
for_each(iv2.begin(), iv2.end(), function1<int>());
cout << endl;
// 2 4
generate_n(first, 3, even_by_two());
for_each(vec.begin(), vec.end(), function1<int>());
cout << endl;
// 6 8 10 3 4 5 6 6 6 6 7 8
partition(first, last, even());
for_each(vec.begin(), vec.end(), function1<int>());
cout << endl;
// 6 8 10 8 4 6 6 6 6 5 7 3
vector<int> vec3(13);
remove_copy(first, last, vec3.begin(), 6);
for_each(vec3.begin(), vec3.end(), function1<int>());
cout << endl;
// 8 10 8 4 5 7 3 0 0 0 0 0 0
vec= { 0, 1,2,3,4,5,6,6,6,6,7,8 };
auto iter = remove_if(first, last, bind2nd(less<int>(), 7));
for_each(vec.begin(), vec.end(), function1<int>());
cout << endl;
cout << iter - first << endl;
// 7 8 2 3 4 5 6 6 6 6 7 8
// 2
vec = { 0, 1,2,3,4,5,6,6,6,6,7,8 };
replace(first, last,6,0);
for_each(vec.begin(), vec.end(), function1<int>());
cout << endl;
// 0 1 2 3 4 5 0 0 0 0 7 8
vec = { 0, 1,2,3,4,5,6,6,6,6,7,8 };
reverse(first, last);
for_each(vec.begin(), vec.end(), function1<int>());
cout << endl;
// 8 7 6 6 6 6 5 4 3 2 1 0
vector<int> vec4(12);
vec = { 0, 1,2,3,4,5,6,6,6,6,7,8 };
reverse_copy(first, last,vec4.begin());
for_each(vec4.begin(), vec4.end(), function1<int>());
cout << endl;
// 8 7 6 6 6 6 5 4 3 2 1 0
vec = { 0, 1,2,3,4,5,6,6,6,6,7,8 };
rotate(vec.begin(), vec.begin() + 5, vec.end());
for_each(vec.begin(), vec.end(), function1<int>());
cout << endl;
// 5 6 6 6 6 7 8 0 1 2 3 4
vec = { 0, 1,2,3,4,5,6,6,6,6,7,8 };
rotate_copy(vec.begin(), vec.begin() + 5, vec.end(), vec3.begin());
for_each(vec3.begin(), vec3.end(), function1<int>());
cout << endl;
// 5 6 6 6 6 7 8 0 1 2 3 4 0 多的空由0填充
vector<int> vec5{ 6,6 };
cout << *(search(vec.begin(), vec.end(),vec5.begin(),vec5.end())+4) << endl;
// 7
cout << *(search_n(vec.begin(), vec.end(), 2, 6, greater<int>())) << endl;
// 7
vec = { 0, 1,2,3,4,5,6,6,6,6,7,8 };
next_permutation(vec.begin(), vec.end());
for_each(vec.begin(), vec.end(), function1<int>());
cout << endl;
// 0 1 2 3 4 5 6 6 6 6 8 7
prev_permutation(vec.begin(), vec.end());
for_each(vec.begin(), vec.end(), function1<int>());
cout << endl;
// 0 1 2 3 4 5 6 6 6 6 7 8
vec = { 0, 1,2,3,4,5,6,6,6,6,7,8 };
cout << count_if(vec.begin(), vec.end(), not1(bind2nd(less<int>(), 6))) << endl;
//6
cout << count_if(vec.begin(), vec.end(), not1(bind1st(greater<int>(), 6))) << endl;
//6