//检索
int main() {
int a[] = { 10, 20, 30, 40 };
auto p1 = find(a, a + 4, 10);
auto p2 = find(a, a + 4, 0);
auto p3 = lower_bound(a, a + 4, 15);//return >= val
auto p4 = upper_bound(a, a + 4, 15);//return > val
cout << p1 - a << endl;
cout << p2 - a << endl;
cout << p3 - a << endl;
cout << p4 - a << endl;
return 0;
}
//集合相关函数
#include <iterator>
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x.begin())
int main() {
vector<int> s1({ 1, 2, 3, 4 });
vector<int> s2({ 2, 3, 4, 5 });
vector<int> su, si, sd, ss;
set_union(ALL(s1), ALL(s2), INS(su));
set_intersection(ALL(s1), ALL(s2), INS(si));
//the elements that are present in the first set, but not in the second one.
set_difference(ALL(s1), ALL(s2), INS(sd));
set_symmetric_difference(ALL(s1), ALL(s2), INS(ss));
for (auto i : su) cout << i << ' '; cout << endl;
for (auto i : si) cout << i << ' '; cout << endl;
for (auto i : sd) cout << i << ' '; cout << endl;
for (auto i : ss) cout << i << ' '; cout << endl;
return 0;
}
//栈,队列,优先队列
//stack: empty, size, top, push, pop, emplace, swap;
//queue: empty, size, front, back, push, pop, emplace, swap;
//priority_queue: empty, size, top, push, pop, emplace, swap;
//assert的应用及随机数的生成
#include <cstdlib>
#include <ctime>
#include <cassert>
void fill_random_int(vector<int> &v, int cnt) {
v.clear();
for (int i = 0; i < cnt; ++i) v.push_back(rand());
}
void test_sort(vector<int> &v) {
sort(v.begin(), v.end());
for (int i = 0; i < v.size() - 1; i++)
assert(v[i] <= v[i + 1]);
}
int main() {
srand(time(NULL));
vector<int> v;
fill_random_int(v, 100000);
test_sort(v);
return 0;
}
第五章 总结
最新推荐文章于 2024-07-20 22:49:13 发布