c++的set容器和map容器

set容器

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
using namespace std;
//set不给有重复的值,但是插入相同的数值不会报错,只是不会插入进去
void test01()
{
	sets;
	set::iterator it;
	it++;
	it--;
	//it + 2;
	//双向迭代器
}
void printset(set& s)
{
	for (set::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << endl;
	}
}
void test02()
{
	sets;
	s.insert(10);
	s.insert(2);
	s.insert(5);
	printset(s);//2 5 10//自身规则进行排序,升序
}
//改变set容器的规则,改为降序
struct myfunc
{
	bool operator()( int val1,  int val2)const//添加 const 限定符,以表明该成员函数不会修改对象的状态
		//否则会报错:具有类型“const myfunc”的表达式会丢失一些 const-volatile 限定符以调用“bool myfunc::operator ()(int,int)”
	{
		return val1 > val2;
	}
};

void printset2(set& s)
{
	for (set::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << endl;
	}
}

void test03()
{
	set s;
	s.insert(10);
	s.insert(2);
	s.insert(5);
	printset2(s);
	s.erase(2);
	printset2(s);
}

//利用算法打印
void print(int val)
{
	cout << val << " ";
}
void test04()
{
	sets;
	s.insert(10);
	s.insert(2);
	s.insert(5);
	for_each(s.begin(), s.end(), print);
	//不能用sort
}
//find(key)//查找键key是否存在,返回该键的迭代器,如不存在,返回set.end();
//lower_bound(keyelem)//返回第一个key>=keyelem元素的迭代器
//upper_bound(keyelem)//返回第一个key>keyelem元素的迭代器
void test05()
{
	sets;
	s.insert(10);
	s.insert(2);
	s.insert(5);
	set::iterator it=s.find(3);
	if (it == s.end())
	{
		cout << "不存在" << endl;;
	}
	else
	{
		cout << " 查找成功" << endl;
	}
	it = s.lower_bound(2);//查找大于等于2的最小数
	if (it == s.end())
	{
		cout << "查找失败" << endl;
	}
	else
	{
		cout << "查找成功:" << *it << endl;
	}
	it = s.upper_bound(2);//查找大于2的最小数
	if (it == s.end())
	{
		cout << "查找失败" << endl;
	}
	else
	{
		cout << "查找成功:" << *it << endl;
	}
	//返回大于等于2的两个最小的数,如果有2那么就返回2和大于2的最小数
	pair::iterator, set::iterator>ret = s.equal_range(2);
	cout << *(ret.first) << endl;
	cout << *(ret.second) << endl;
	multisets1;
	s1.insert(4);
	s1.insert(4);
	s1.insert(4);
	s1.insert(2);
	s1.insert(3);
	cout << s1.count(4) << endl;
}
class maker
{
public:
	maker(string name, int age)
	{
		this->name = name;
		this->age = age;
	}
public:
	string name;
	int age;
};
//存储对象时,要告诉set规则
struct makerfunc
{
	bool operator()(const maker& m1, const maker& m2)const
	{
		//return m1.age > m2.age;
		return m1.name>m2.name;
	}
};
void test06()
{
	sets;
	s.insert(maker("aaa", 53));
	s.insert(maker("bbb", 20));
	s.insert(maker("ccc", 33));
	for (set::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << (*it).name << " " << (*it).age << endl;
	}
}
int main()
{
	test06();
	system("pause");
	return 0;
}

map容器

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
using namespace std;
void test01()
{
	map::iterator it;
	it++;
	it--;
	//it + 2;//err,双向迭代器
}
template 
void printmap(T& m)
{
	for (map::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key:" << (*it).first << " " << "value:" << (*it).second << endl;
	}
}
struct myfunc
{
	bool operator()(int ker1, int key2)const
	{
		return ker1 > key2;
	}
};
void test02()
{
	mapmymap;
	//1:
	mymap.insert(pair(3, "aaa"));
	//2:
	mymap.insert(make_pair(6, "bbb"));
	mymap.insert(make_pair(3,"ccc"));//不允许key值一样的
	//3:
	mymap[4] = "ddd";//4是键值,ddd是实值
	printmap(mymap);
}
//[]方式存入数据,如果没有实值,那么键值也是存在的
void test03()
{
	mapmymap;	
	mymap.insert(pair(3, "aaa"));	
	mymap.insert(make_pair(6, "bbb"));	
	mymap[4] = "ddd";
	printmap(mymap);
	cout << "size:" << mymap.size() << endl;//3
	mymap[100];//插入键值,返回实值
	cout << "size:" << mymap.size() << endl;//4
}
void test04()
{
	mapm;
	m[1] = "aaa";
	m[3] = "ccc";
	m[6] = "bbb";
	map::iterator it = m.find(3);
	
	if (it == m.end())
	{
		cout << "查找失败" << endl;
	}
	else
	{
		cout << "查找成功" << "key:" << it->first << " " << "value:"<second << endl;
	}
	it = m.lower_bound(3);//键值大于等于3

	if (it == m.end())
	{
		cout << "查找失败" << endl;
	}
	else
	{
		cout << "查找成功" << "key:" << it->first << " " << "value:" << it->second << endl;
	}
	it = m.upper_bound(3);//键值大于3

	if (it == m.end())
	{
		cout << "查找失败" << endl;
	}
	else
	{
		cout << "查找成功" << "key:" << it->first << " " << "value:" << it->second << endl;
	}
	pair::iterator, map::iterator>ret = m.equal_range(3);
	if (ret.first != m.end()) {
		cout << "key:" << ret.first->first << "value:" << ret.second->second << endl;
	}
	if (ret.second != m.end()) {
		cout << "key:" << ret.second->first << "value:" << ret.second->second << endl;
	}
}
int main()
{
	test04();
	system("pause");
	return 0;
}

你可能感兴趣的:(c++,开发语言)