set,map简单介绍(使用介绍)

一 . 序列式容器,关联式容器

vector、list、deque、forward_list(C++11)等,这些容器统称为序列式容器,因为其底层为线性序列的数据结构,里面存储的是元素本身。
关联式容器也是用来存储数据的,与序列式容器不同的是,其里面存储的是<key, value>结构的
键值对,在数据检索时比序列式容器效率更高。

二 . set

set - C++ Reference

1.迭代器

 

注: 但*it = 10;报错,set不支持修改;

 

2 . insert (使set具有去重的功能)

void test1()
{
	set<int> s;
	s.insert(3);
	s.insert(1);
	s.insert(7);
	s.insert(5);

	set<int>::iterator get;
	std::pair<set<int>::iterator, bool> cnt1 = s.insert(5);
	auto cnt2 = s.insert(5);

	bool ret = s.insert(3).second;
	cout << ret << endl;
	get = s.insert(3).first;
	cout << *get << endl;

	cout << "-----------" << endl;

	ret = s.insert(2).second;
	cout << ret << endl;
	get = s.insert(2).first;
	cout << *get << endl;

	set<int>::iterator it = s.begin();
	while (it != s.end())
	{
		cout << *it << "_";
		it++;
	}
	cout << endl;
}

 3 . erase,find

void test2()
{
	set<int> s;
	s.insert(3);
	s.insert(1);
	s.insert(2);
	s.insert(7);
	s.insert(5);

	s.erase(2);
	/*或者*/
	set<int>::iterator it = s.find(3);
	if (it != s.end())
	{
		s.erase(it);
	}

	it = s.erase(30);
	
	it = s.begin();
	while (it != s.end())
	{
		cout << *it << "_";
		it++;
	}
	cout << endl;
}

 4 . lower_bound, upper_bound


void test3()
{
	std::set<int> myset;
	std::set<int>::iterator itlow, itup;

	for (int i = 1; i < 10; i++) myset.insert(i * 10); // 插入 10 20 30 40 50 60 70 80 90

	itlow = myset.lower_bound(30);    //如果是25,itlow依旧是30的iterator(返回 >= val位置的迭代器) 
	//itlow = myset.lower_bound(25);
	itup = myset.upper_bound(60);//实际是70的iterator    //如果是65,依旧是70(返回 > val 的迭代器)
	//itup = myset.upper_bound(70);

	myset.erase(itlow, itup);        //删除后结果是 10 20 70 80 90

	//遍历
	std::cout << "myset contains:";
	for (std::set<int>::iterator it = myset.begin(); it != myset.end(); ++it)
		std::cout << ' ' << *it;
	std::cout << endl;

}

5 . equal_range

int main ()
{
  std::set<int> myset;

  for (int i=1; i<=5; i++) myset.insert(i*10);   // myset: 10 20 30 40 50

  std::pair<std::set<int>::const_iterator, std::set<int>::const_iterator> ret;
  ret = myset.equal_range(30);//equal_range返回类型是pair<iterator, iterator>

  std::cout << "the lower bound points to: " << *ret.first << '\n';
  //*ret.first = 30 ( >= val)
  std::cout << "the upper bound points to: " << *ret.second << '\n';
  //*ret.second = 40 ( > val)
  return 0;
}

6 . count

size_type = size_t 

有多少个val,返回多少。没有val,返回0.

 

三 . multiset

大致上功能和set差不多,但可以插入相同的值

#include<set>
void test4()
{
	multiset<int> ms;
	ms.insert(8);
	ms.insert(1);
	ms.insert(4);
	ms.insert(3);
	ms.insert(8);
	ms.insert(2);
	ms.insert(2);

	multiset<int>::iterator it = ms.begin();
	while (it != ms.end())
	{
		cout << *it << "_";
		it++;
	}
	cout << endl;
}

 

区别: 

1. find找的是中序的第一个val

2. equal_range在multiset更有意义

删除所有2

3 . count,能知道有多少val 

 

四 . map

1 . insert,迭代器

void test5()
{
	map<string, string> dict;
	dict.insert(std::pair<string, string>("sort", "排序"));
	dict.insert(std::pair<string, string>("insert", "插入"));
	dict.insert(std::pair<string, string>("left", "左边"));

	dict.insert(make_pair("right", "右边"));

	map<string, string>::iterator it = dict.begin();
	while (it != dict.end())
	{
		cout << it->first << ":" << it->second << endl;
		//两种写法都可以
		//cout << (*it).first << ":" << (*it).second << endl;
		//first是Key的值,second是Value的值
		it++;
	}
	cout << endl;
}

注:只能修改 second(Value)的值,不能修改 first(Key)的值

Key前有const修饰,所以不能改。

 

2 . operator[]

3.multimap同map,但不支持operator[],因为可以有多个Key值。 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值