第十二章 set和multiset

set和multiset插入,删除,查找都是O(log n)复杂度的。 set不允许重复 multiset则允许。

#include "iostream"
#include "map"
#include "set"
using namespace std;
int main() {
	multiset<int>st;
	int a[10] = { 1,14,12,13,7,13,21,19,8,8 };
	for (int i = 0; i < 10; i++) {
		st.insert(a[i]);
	}
	multiset<int>::iterator i;
	for (i = st.begin(); i != st.end(); i++) {
		cout << *i <<" ";
	}
	cout << endl;
	i = st.find(22);
	if (i == st.end()) {
		cout << "Not Found" << endl;
	}
	st.insert(22);
	i = st.find(22);
	if (i == st.end()) {
		cout << "Not Found" << endl;
	}
	else {
		cout << "Found" << endl;
	}
	i = st.lower_bound(13); // 返回最靠后的迭代器it [begin(),it)中的元素
	cout << *i << endl;
	i = st.upper_bound(8); //返回最靠前的迭代器it [it,end())中的元素都在8后面
	cout << *i << endl;
	st.erase(st.find(13));
	for (i = st.begin(); i != st.end(); i++) {
		cout << *i << " ";
	}
	cout << endl;
	return 0;
}

/* 在vs2015中跑会出错 用在线编译器没问题 不知道为啥- -是支持的C++标准不一样么?*/
#include <iostream>
#include <functional>
#include <set>
using namespace std;
struct Rule1 {
	bool operator()(const int & a, const int & b) {
		return (a % 10) < (b % 10);
	}//返回值为true则说明a必须排在b前面
};
int main() {
	multiset<int, greater<int> > st; //排序规则为从大到小
	int a[10] = { 1,14,12,13,7,13,21,19,8,8 };
	for (int i = 0; i < 10; ++i)
		st.insert(a[i]);
	multiset<int, greater<int> >::iterator i;
	for (i = st.begin(); i != st.end(); ++i)
		cout << *i << ",";
	cout << endl;
	multiset<int, Rule1 > st2;
	//st2的元素排序规则为:个位数小的排前面
	for (int i = 0; i < 10; ++i)
		st2.insert(a[i]);
	multiset<int, Rule1>::iterator p;
	for (p = st2.begin(); p != st2.end(); ++p)
		cout << *p << ",";
	cout << endl;
	p = st2.find(133); /* 能找到 因为不能确定是133排在13前面还是13排在133前面 所以即找到 输出13*/
	cout << *p << endl;
	return 0;
}

/* 在vs2015中跑会出错 用在线编译器没问题 不知道为啥- -是支持的C++标准不一样么?*/
#include <iostream>
#include <functional>
#include <set>
using namespace std;
struct Student {
	char name[20];
	int id;
	int score;
};
Student students[] = { { "Jack",112,78 },{ "Mary",102,85 },{ "Ala",333,92 },{ "Zero",101,70 },{ "Cindy",102,78 } };
struct Rule {
	bool operator() (const Student& s1, const Student& s2) {
		if (s1.score != s2.score)
			return s1.score > s2.score;
		else
			return (strcmp(s1.name, s2.name) < 0);
	}
};
int main() {
	multiset<Student, Rule>st;
	for (int i = 0; i < 5; i++) {
		st.insert(students[i]);
	}
	multiset<Student, Rule>::iterator p;
	for (p = st.begin(); p != st.end(); p++) {
		cout << p->score << " " << p->name << " " << p->id << endl;
	}
	Student s = { "Mary",1000,85 };
	p = st.find(s);
	if (p != st.end()) {
		cout << p->score << " " << p->name << " " << p->id << endl;
	}
	return 0;
}

/* 在vs2015中跑会出错 用在线编译器没问题 不知道为啥- -是支持的C++标准不一样么? */
#include <iostream>
#include <functional>
#include <set>
using namespace std;
int main() {
	set<int>st;
	int a[10] = { 1,2,3,8,7,7,5,6,8,12 };
	for (int i = 0; i < 10; i++) {
		st.insert(a[i]);
	}
	cout << st.size() << endl;
	set<int>::iterator i;
	for (i = st.begin(); i != st.end(); i++) {
		cout << *i << " ";
	}
	cout << endl;
	pair<set<int>::iterator, bool>result = st.insert(2);
	if (!result.second) {
		cout << *result.first << " " << "already exist" << endl;
	}
	else {
		cout << *result.first << " " << "inserted" << endl;
	}
	return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值