第五章 总结

//检索
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值