【C++】STL库的multiset容器


  1. multiset是库中一个非常有用的类型;
  2. multiset能时刻保证序列中的数是有序的,而且序列中可以存在重复的数
  3. 在multiset在,元素的value也会识别它组成的键值对;
  4. multiset元素的值不能在容器中进行修改,但可以插入和删除
  5. multiset容器通过key访问单个元素比unordered_multiset容器慢,但当使用迭代器遍历的时候,会得到一个有序序列.
  6. multiset的底层是二叉搜索树(红黑树)
  7. multiset与set的区别是multiset中的元素可以重复
  8. 使用迭代器遍历,可以得到有序的multiset
  9. multiset中的元素不可修改
  10. multiset的查找的时间复杂度是Olog2N
  11. multiset的作用,可以对元素进行排序
  12. 头文件
#include< set >

1、构造函数

multiset m0;
multiset m = { 3,5,9,4,6,55,11 };

#include "stdafx.h"
#include <iostream>
#include <set>
#include<functional>
using namespace std;


int main()
{
	multiset<int> m0;
	cout << m0.size() << endl; // 0

							   // 默认排序:升序
	multiset<int> m = { 3,5,9,4,6,55,11 };
	cout << m.size() << endl; // 7
	for (auto it = m.begin(); it != m.end(); ++it) {
		cout << *it << " "; // 3 4 5 6 9 11 55
	}

	// 仿函数:降序
	multiset<int, greater<int>> m1 = { 3,5,9,4,6,55,11 };
	for (auto it = m1.begin(); it != m1.end(); ++it) {
		cout << *it << " "; // 55 11 9 6 5 4 3
	}

	// 仿函数:升序
	multiset<int, less<int>> m2 = { 3,5,9,4,6,55,11 };
	for (auto it = m2.begin(); it != m2.end(); ++it) {
		cout << *it << " "; // 3 4 5 6 9 11 55
	}

	// 构造函数:[first,last]
	multiset<int> m3(m2.begin(), m2.end());
	for (auto it = m3.begin(); it != m3.end(); ++it) {
		cout << *it << " "; // 3 4 5 6 9 11 55
	}
	return 0;
}

2、增

insert () 不去重+排序

int main()
{
	multiset<int> m;
	m.insert(9);
	m.insert(45);
	m.insert(3);
	m.insert(22);

	m.insert(9);
	m.insert(45);
	m.insert(3);
	m.insert(22);

	for (auto it = m.begin(); it != m.end(); ++it) {
		cout << *it << " "; // 3 3 9 9 22 22 45 45
	}
	return 0;
}

emplace()

int main()
{
	multiset<int> m;
	m.emplace(9);
	m.emplace(45);
	m.emplace(3);
	m.emplace(22);
	 
	m.emplace(9);
	m.emplace(45);
	m.emplace(3);
	m.emplace(22);

	for (auto it = m.begin(); it != m.end(); ++it) {
		cout << *it << " "; // 3 3 9 9 22 22 45 45
	}
	return 0;
}

emplace_hint()

3、删

erase()

int main()
{
	multiset<int> m = {3,5,1,9,4}; 
	auto it_next = m.erase(m.begin());
	cout << *it_next << endl; // 3
	for (auto it = m.begin(); it != m.end(); ++it) {
		cout << *it << " "; // 3 4 5 9
	}

	multiset<int> m1 = { 3,5,1,9,4 ,3,5,1,9,4 };
	int cnt = m1.erase(9);
	cout << cnt << endl; // 2
	for (auto it = m1.begin(); it != m1.end(); ++it) {
		cout << *it << " "; // 1 1 3 3 4 4 5 5
	}

	multiset<int> m2 = { 3,5,1,9,4 ,3,5,1,9,4 }; //1 1 3 3 4 4 5 5 9 9
	m2.erase(++m2.begin(),--m2.end());
	for (auto it = m2.begin(); it != m2.end(); ++it) {
		cout << *it << " "; // 1 9
	}

	return 0;
}

clear()

4、改

swap()

int main()
{
	multiset<int> m = {3,5,1,9,4};
	multiset<int> m1 = { 4,4,4,5,5,5 };

	swap(m,m1);

	for (auto it = m.begin(); it != m.end(); ++it) {
		cout << *it << " "; // 4 4 4 5 5 5
	}
	cout << endl;

	for (auto it = m1.begin(); it != m1.end(); ++it) {
		cout << *it << " "; // 1 3 4 5 9
	}


	return 0;
}

5、查

find()

返回元素值为val的第一个元素,如果没有返回end()

int main()
{
	// find(val) 返回第一个val值的迭代器
	multiset<int> m = {3,3,3,5,1,9,3,4};
	for (auto it = m.begin(); it != m.end(); ++it) {
		cout << *it << " "; // 1 3 3 3 3 4 5 9 
	}
	cout << endl;
	auto it_pos = m.find(3);
	for (auto it = it_pos; it != m.end(); ++it) {
		cout << *it << " "; // 3 3 3 3 4 5 9
	}
	return 0;
}

count()

返回multiset中值为val的元素的个数

lower_bount()

在这里插入图片描述

s.lower_bount(val);
返回元素值为val的第一个可安插位置,也就是元素值 >= val的第一个元素位置

upper_bound()

s.upper_bound(val);
返回元素值为val的最后一个可安插位置,也就是元素值 > val的第一个元素位置

equal_range()

s.equal_range(val);
返回val可安插的第一个位置和最后一个位置,也就是元素值==val的区间

6、判空 empty()

7、大小

size()

max_size()

返回最大容量个数。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值