容器应急使用 vector deque list map set

5个容器的部分函数
 vectordequelistmapset
assign

1

11  
at11 1 
back111  
begin11111
capacity1    
cbegin11111
cend11111
clear11111
count   11
crbegin11111
crend11111
data1    
emplace11111
emplace_back111  
emplace_front 11  
emplace_hint   11
empty11111
end11111
equal_range   11
erase11111
find   11
front111  
get_allocator11111
insert11111
key_comp   11
lower_bound   11
max_size11111
merge  1  
pop_back111  
pop_front 11  
push_back111  
push_front 11  
rbegin11111
remove  1  
remove_if  1  
rend11111
reserve1    
resize111  
reverse  1  
shrink_to_fit11   
size11111
sort  1  
splice  1  
swap11111
unique  1  
upper_bound   11
value_comp    1

通用类 红字   8个定位begin end rbegin rend cbegin cend crbegin crend

                       元素 增emplace insert  删erase clear 交换swap 

                      获得一些成员  空empty  重分配get_allocator 内存最大空间max_size 元素个数size 

序列式容器  设置元素assign 最后一个元素back 后添加emplace_back 前添加emplace_front 第一个元素front 后弹出pop_back 前弹出pop_front 后添加push_back 前添加push_front 改变元素个数 resize 自适应空间 shrink_to_fit

关系式容器 检测是否存在count 添加数据 emplace_hint 找大概位置equal_range 找值位置find 关键字比大小 key_comp    ??lower_bound  upper_bound

混杂   获得目标位置的值at

唯一 绿字 vector 容量大小capacity  首地址??data  更改容器大小reserve

               list   剪切 merge 反序 reverse 清除某个值remove 清除特殊值remove_if 按指定要求排序sort  转移数据splice  按指定要求删除数据 unique 

              set  值比大小value_comp

 

#include<stdio.h>
#include<vector>
#include<deque>
#include<list>
#include<map>
#include<set>
using namespace std;

bool Isbase(int a)
{
	if (a % 2)
		return true;
	return false;
}

bool isMax(int a, int b)
{
	return !(a > b);
}
//5个容器的应急使用方法 vector deque list map set (multiset multimap允许重复关键字)
int main()
{
	//stl 标准模板库 standard template library
	//C++提供了模板,模板的应用引出了泛型编程/无视类型的编程方法/void无类型=所有类型<---------内存中只有01,没有类型,所以指针是个bug级的存在
	//由于部分内容需要大量使用,如果没有统一标准会导致两点
	//1. 如果使用就要拷贝原文件,源文件的编写者的产权,一些机密内容无法保护
	//2. 后来者需要熟悉前人留下的设计模式,然后才能使用,适应期长
	//标准库的出现解决这个问题,然后此次的重点放在容器类的使用				<-------说到底容器只是一个概念,为了统一游戏规则,给了一个标准库,凡是符合概念的都可以称之为容器,或者一种数据结构,比如栈
	//容器类,就是一个箱子,箱子里面放数据,放数据肯定是有一定依据的-》数据的组成方式 = 数据结构
	//既然是容器,那么有一些函数是通用的/都有的
	//容器分成两种 序列式容器,逻辑连续, vector deque list   关系式容器,数据关系连续 set map (multiset multimap)
	//关系式容器多了一个排序的功能,并不意味着容器是用来排序的

	//容器要求
	// 数据采用外部数据外部处理,内部数据内部处理,即别人的数据你别动,你的数据自己释放,所以容器进行元素添加时,内部实现的是拷贝,每一个容器内的每一个元素都要具备被拷贝的能力
	//引用不具备拷贝能力,出生既是马甲绑定   int a=1; 隐式拷贝构造  int a(1); 显式拷贝构造
	//元素具有一定的次序,这个次序的概念很广,可以是逻辑顺序(栈,队列),物理位置连续(数组),大小关系一致(map) 等等等(... 代表任意类型)
	//一般而言,各项操作都不是安全的	<-----------C的bug级存在,指针越界
	//由于各项操作都不安全,所以要提供一个判断是否可以的接口/函数---->设计容器,发生的可以预见但是处理不了的情况,a异常,b不管。 谁调用谁出错谁负责

	//通用函数/接口
	//出生,回归,元素个数,同容器的关系,容器交换,迭代器(智能指针),增删改查
	//具体的思想等可以看MyVector的代码,就是各种概念,概念落地形成代码

	vector<int> a1;			//动态数组
	deque<int> a2;			//双动态数组 _front _______1 0 1________  _back
	list<int> a3;			//链表
	map<int, double> a4;	//多参数
	set<int> a5;			//单参数
	multiset<int> a6;		//set可重
	multimap<int, double> a7;//map可重
	vector<int> b1;
	for (int i = 0; i < 10; i++)
	{
		b1.push_back(i);
		a2.push_front(i);
		a4[i] = (i + 3.14);
	}
	a4[10] = 3.14;
	vector<int>::iterator it1;
	vector<int>::iterator it2;
	vector<int>::iterator it3;
	it1 = b1.begin();
	it2 = b1.begin() + 4;

	//assign 用于给容器赋值,参数列表以vector为例
	//void assign(std::vector<int,std::allocator<int>>::size_type_Count,const std::vector<int,std::allocator<int>>::value_type &_Val)
	//传入count,Val 即容器变成n个srcData 
	//void enable_if<std::_Is_iterator<_Iter>::value,vodi>::type assign<_Iter>(_Iter _First,_Iter _Last)
	//传入两个智能指针/迭代器 ,拷贝两个指针之间的数据,反了跳出程序	std::length_error at memory location 某个地址
	//void assign(std::initializer_list<std::vector<int,std::allocator<int>>::value_type>_list
	//初始化列表
	a1.assign(3, 6);
	a2.assign(3, 7);
	a3.assign(3, 8);
	a1.assign(it1, it2);
	a1.assign({ 1, 2, 3, 4, 5 });
	//at 返回目标位置的值,两种用法,注意参数,一个是普通参数reference,一个是常量参数(内容不可修改)const_reference
	//std::vector<int,std::allocator<int>>::reference at(std::vector<int,std::allocator<int>>::size_type_Pos)
	//返回目标位置的值			位置过大std::out_of_range at memory location 某个地址
	//std::vector<int,std::allocator<int>>::const_reference at(std::vector<int,std::allocator<int>>::size_type_Pos)const
	a1.at(1);
	a2.at(2);
	a4.at(3);		//断点调试,直接监视a4.at 卡一会然后vs直接关闭,at比对的是第一个关键字的值,如果元素不存在,报错abort() has been calld
	printf("%lf\n", a4.at(10));
	//a1.at();

	//back 返回最后一个元素,两种用法,同at
	//std::vector<int,std::allocator<int>>::reference back()
	//std::vector<int,std::allocator<int>>::const_reference back()const
	a1.assign(3, 6);
	printf("%d\n", a1.back());
	a2.back();
	a3.back();

	//begin 返回第一个元素的位置 智能指针 ,两种用法,同at
	//std::vector<int,std::allocator<int>>::const_iterator begin()const
	//std::vector<int,std::allocator<int>>::iterator begin()
	it1 = a1.begin();
	a2.begin();
	a3.begin();
	a4.begin();
	a5.begin();

	//capacity 返回 vector 的容量
	//std::vector<int,std::allocator<int>>::size_type capacity()const
	a1.capacity();

	//cbegin const_iterator类型,相当于一个记忆符号,可是一旦对容器进行增删交换,可能导致内存的重分配,然后与之相关的迭代器都会变动,可能设计之初是用于常量记录位置
	//std::vector<int,std::allocator<int>>::const_iterator cbegin()const
	vector<int>::const_iterator it4 = a1.cbegin();
	vector<int>::const_iterator it5 = a1.begin();
	a1.cbegin();
	a2.cbegin();
	a3.cbegin();
	a4.cbegin();
	a5.cbegin();

	//cend const_iterator类型,同cbegin,逾尾结点
	//std::vector<int,std::allocator<int>>::const_iterator cend()const
	a1.cend();
	a2.cend();
	a3.cend();
	a4.cend();
	a5.cend();
	a1.cend();


	//clear 清空容器中的所有元素,容器大小不变
	a1.clear();
	a2.clear();
	a3.clear();
	a4.clear();
	a5.clear();

	//count 判断容器是否存在此数据,按第一关键字进行搜索,注意类型
	//std::_Tree<std::_Tmap_traits<int,double,std::less<int>,std::allocator<std::pair<const int,double>>,false>>size_type count(const std::_Tree<std::_Tmap_traits<int,double,std::less<int>,std::allocator<std::pair<const int,double>>,false>>::key_type &keyval)const
	for (int i = 0; i < 10; i++)
		a4[i] = (i + 3.14);
	if (a4.count(2))
		printf("have\n");
	else
		printf("no\n");

	//crbegin 同cbegin,r逆向计算
	//std::vector<int,std::allocator<int>>::const_iterator crbegin()const
	a1.crbegin();
	a2.crbegin();
	a3.crbegin();
	a4.crbegin();
	a5.crbegin();

	//crend 同crbegin
	//std::vector<int,std::allocator<int>>::const_iterator crend()const
	a1.crend();
	a2.crend();
	a3.crend();
	a4.crend();
	a5.crend();

	//data 获得首地址值???
	a1.assign(3, 6);
	if (a1.data())
		printf("yes\n");

	//emplace 就是一个插入数据,避免产生不必要的临时变量---》拷贝还是赋值的问题
		//std::vector<_Ty,Alloc>::iterator emplace<_Valty...>(std::vector<_Ty,_Alloc>::const_iterator_Where,_Valty &&..._Val)
			//智能指针告诉位置, val值
		//std::_Tree<_Traits>::_Pairib emplace<_Valty...>(_Valty &&..._Val)			map set注意类型匹配
	it1=a1.begin();
	a1.assign(3, 6);
	a1.emplace(it1,5);
	a4.emplace(15,3.14);
	deque<int>::iterator itd1 = a2.begin();
	a2.emplace(itd1,5);
	list<int>::iterator itl1 = a3.begin();
	a3.emplace(itl1,1);
	a5.emplace(7);
	
	//emplace_back 后插入
		//void emplace_back<_Valty...>(_Valty &&..._Val)
	a1.emplace_back(1);
	a2.emplace_back(2);
	a3.emplace_back(3);

	//emplace_front 前插入
		//void emplace_back<_Valty...>(_Valty &&..._Val)  vector是动态数组,没法前插入
	a2.emplace_front(3); 
	a3.emplace_front(2);

	//emplace_hint
		//std::_Tree<_Traits>::iterator emplace_hint<_Valty...>(std::_Tree<_Traits>::const_irerator_Where,Valty &&..._Val)  找位置,或者在位置处插入值
		//多参数可能不明显	http://www.cplusplus.com/reference/set/set/emplace_hint/
	map<int, double>::iterator itm1 = a4.begin();
	map<int, double>::iterator itm2;
	itm2=a4.emplace_hint(itm1,17,9.9);
	a5.emplace_hint(a5.cend(),8);


	//empty 检测容器是否为空
	a1.empty();
	a2.empty();
	a3.empty();
	a4.empty();
	a5.empty();

	//end 智能指针定位逾尾处
	it1=a1.end();
	a2.end();
	a3.end();
	itm1=a4.end();
	a5.end();

	//equal_range 找到key值元素的位置,及附近位置,如果没有会有一个范围值
		//std::_Tree<std::_Tmap_traits<int,double,std::less<int>,std::allocator<std::pair<const int,double>>,false>>::_Paircc equal_range(const std::_Tree<stdLL_Tmap_traits<int,double,std::less<int>,std::allocator<std::pair<const int,double>>,false>>::key_type &_Keyval)const
		//std::_Tree<std::_Tmap_traits<int,double,std::less<int>,std::allocator<std::pair<const int,double>>,false>>::_Pairii equal_range(const std::_Tree<stdLL_Tmap_traits<int,double,std::less<int>,std::allocator<std::pair<const int,double>>,false>>::key_type &_Keyval)
			//返回的是pair
	map<int,double>::_Paircc ret;		//又两个成员,first second
	ret=a4.equal_range(17);
	a5.equal_range(1);

	//erase 
		//std::vector<int,std::allocator<int>>::iterator erase(std::vector<int,std::allocator<int>>::const_iterator _First_arg,std::vector<int,std::allocator<int>>::const_iterator _Last_arg)
			//清除一段数据
		//std::vector<int,std::allocator<int>>::iterator erase(std::vector<int,std::allocator<int>>::const_iterator _Where);
			//清除迭代器位置的数据
		//std::_Tree<std::_Tmap_traits<int,double,std::less<int>,std::allocator<std::pair<const int,double>>,false>>::size_type erase(const std::_Tree<std::_Tmap_traits<int,double,std::less<int>,std::allocator<std::pair<const int,double>>,false>>::key_type &_Keyval
			//清除关键字
		//std::_Tree<std::_Tmap_traits<int,double,std::less<int>,std::allocator<std::pair<const int,double>>,false>>::iterator erase(std::_Tree<std::_Tmap_traits<int,double,std::less<int>,std::allocator<std::pair<const int,double>>,false>>::const_iterator _First,std::Tree<std::_Tmap_traits<int,double,std::less<int>,std::allocator<std::pair<const int,double>>,false>>::const_iterator _Last)
			//清除一段数据
		//std::_Tree<std::_Tmap_traits<int,double,std::less<int>,std::allocator<std::pair<const int,double>>,false>>::iterator erase(std::_Tree<std::_Tmap_traits<int,double,std::less<int>,std::allocator<std::pair<const int,double>>,false>>::const_iterator _Where)
			//清除迭代器位置的值
	it1=a1.begin();
	it2=a1.end();
	a1.erase(it1, it2);
	itd1 = a2.begin();
	a2.erase(itd1);
	itl1 = a3.begin();
	a3.erase(itl1);
	a4.erase(a4.begin());
	a5.erase(a5.begin());		//end是逾尾

	//find	返回迭代器,找key值,如果没有,迭代器不变,一个是常量迭代器,一个普通迭代器
		//std::_Tree<std::_Tmap_traits<int,double,std::less<int>,std::allocator<std::pair<const int,double>>,fasle>>::const_iterator find(const std::_Tree<std::_Tmap_traits<int,double,std::less<int>,std::allocator<std::pair<const int,double>>,false>>::key_type &_Keyval)const
		//std::_Tree<std::_Tmap_traits<int,double,std::less<int>,std::allocator<std::pair<const int,double>>,fasle>>::iterator find(const std::_Tree<std::_Tmap_traits<int,double,std::less<int>,std::allocator<std::pair<const int,double>>,false>>::key_type &_Keyval)
	itm1=a4.find(20);

	//front 返回第一个元素
		//std::vector<int,std::allocator<int>>::const_reference front() const 还有一个没有const的
	a1.assign(3, 6);
	a1.front();
	a2.front();
	a3.front();

	//get_allocator 内存模板
	a1.get_allocator();
	a2.get_allocator();
	a3.get_allocator();
	a4.get_allocator();
	a5.get_allocator();

	//insert 在指定位置插入等,同emplace
		//两个迭代器,一个前一个后
		//一个迭代器位置,n个 val
		//一个迭代器位置,一个val		const int &_Val
		//一个迭代器位置,一个初始化列表--》assign
		//与第三个不知道有什么区别		int &&_Val
		//map set可返回pair iterator
		//一个初始化列表 
		//数组头尾
		//迭代器位置,Val值
		//Val值 pair构造
	a1.insert(a1.end(),{5,4,3,2,1});
	a2.insert(a2.begin(),77);
	a3.insert(a3.begin(),66);
	//a4.insert(pair<int,double>(13,4.44));
	//a5.insert(7);

	//key_comp   key_compare key_comp() const 不知道干什么的
	map<int,double>::key_compare tt;
	tt=a4.key_comp();
	a4.clear();
	a4.insert({ { 1, 4.44 }, { 2, 2.2 } });
	if(tt(2,1))
	a4.key_comp()(9,2);

	//lower_bound 除非直接指向,否则不会是其本身??
	a4.lower_bound(1);
	a5.lower_bound(3);

	//max_size 内存可开辟最大空间
	a1.max_size();
	a2.max_size();
	a3.max_size();
	a4.max_size();
	a5.max_size();

	//merge  两个链表按照相同数据大小关系排列,然后把参数链表b3的所有数据转移并按照相同数据关系插入到a3中
	list<int> b3;
	a3.erase(a3.begin());
	b3=a3;
	a3.merge(b3);

	//pop_back 弹出最后一个元素
	a1.pop_back();
	a2.pop_back();
	a3.pop_back();

	//pop_front 弹出第一个元素
	a2.pop_front();
	a3.pop_front();

	//push_back 后添加
	a1.push_back(1);
	a2.push_back(1);
	a3.push_back(1);

	//push_front 前添加
	a2.push_front(2);
	a3.push_front(2);

	//rbegin 返回迭代器位置,逆向排序第一个
	a1.rbegin();
	a2.rbegin();
	a3.rbegin();
	a4.rbegin();
	a5.rbegin();

	//remove 清除掉某一个值的所有数据
	a3.remove(1);

	//remove_if(op)  op是一个函数指针,表示判断关系,单参数
	a3.remove_if(Isbase);

	//rend 返回迭代器,逆向排序最后一个
	a1.rend();
	a2.rend();
	a3.rend();
	a4.rend();
	a5.rend();

	//reserve 更改vector容器大小,只变大,不变小
	a1.reserve(11);

	//resize 改变元素个数,截取或者补零
	a1.resize(16);
	a2.resize(16);
	a3.resize(16);

	//reverse 反序
	a3.reverse();

	//shrink_to_fit 去除掉没有使用的空间,0也是元素,结果capacity=len
	a1.reserve(100);
	a1.shrink_to_fit();
	a2.shrink_to_fit();

	//size 返回元素个数
	a1.size();
	a2.size();
	a3.size();
	a4.size();
	a5.size();

	//sort 排序,默认从小到大,可以添加函数指针,两个参数
	a3.sort();

	//splice 把另一个容器的所有数据转移到这个容器的指定位置
			//迭代器位置,另一个容器
			//迭代器位置,两个迭代器前后
	a3.splice(itl1,b3);

	//swap 交换两个容器
	a1.swap(b1);
	deque<int> b2;
	a2.swap(b2);
	a3.swap(b3);
	map<int, double> b44;
	a4.swap(b44);
	set<int> b5;
	a5.swap(b5);

	//unique 默认清除连续相同的元素  unique(op) op函数指针,两个参数
	a3.unique(isMax);

	//upper_bound 同lower_bound
	a4.upper_bound(15);
	a5.upper_bound(15);

	//value_comp 同key_comp 只不过是属于set的
	a5.value_comp();

	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值