前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >STL标准库容器

STL标准库容器

作者头像
废江_小江
发布2022-09-05 11:52:29
2490
发布2022-09-05 11:52:29
举报
文章被收录于专栏:总栏目

STL对定义的通用容器分三类:顺序性容器、关联式容器和容器适配器。 顺序性容器:vector、deque、list 关联性容器:set、multiset、map、multimap 容器适配器:stack、queue

这里主要学习顺序容器和容器适配器

向量容器

代码语言:javascript
复制
//【例13.5】的程序:向量容器的使用
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
	vector<int> v(3);						//定义初始长度为3的整数容器
	v[0]=5;									//下标0处放置元素5
	v[1]=2;									//下标1处放置元素2
	v.push_back(7);							//在尾部插入元素7
	vector<int>::iterator first=v.begin();	//让first指向开头元素
	vector<int>::iterator last=v.end();		//让last指向尾部元素
	while (first!=last)						//循环输出所有元素
		cout << *first++ << " ";
	cout << endl;
	return 1;
}

双端队列容器

代码语言:javascript
复制
//【例13.6】的程序:双端队列容器的使用
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
void disp(deque<int> &dq);
int main()
{
	deque<int> dq;			//建立一个双端队列dq
	dq.push_front(1);			//队头插入1
	dq.push_back(2);			//队尾插入2
	dq.push_front(3);			//队头插入3
	dq.push_back(4);			//队尾插入4
	disp(dq);
	dq.pop_front();			//删除队头元素
	dq.pop_back();			//删除队尾元素
	disp(dq);
	return 1;
}
void disp(deque<int> &dq)
{
	deque<int>::iterator iter;
	for (iter=dq.begin();iter!=dq.end();iter++)
		cout << *iter << " ";
	cout << endl;
}

链表容器

代码语言:javascript
复制
//【例13.7】的程序:链表容器的使用
#include <iostream>
#include <list>
using namespace std;
int main()
{    list<int> lst;
	list<int>::iterator i,start,end;
	lst.push_back(5); lst.push_back(2); lst.push_back(4);
	lst.push_back(1); lst.push_back(3); lst.push_back(8);
	lst.push_back(6); lst.push_back(7);
	cout << "lst: ";
	for (i=lst.begin();i!=lst.end();i++)
		cout << *i << " ";
	cout << endl;
	i=lst.begin();
	start=++lst.begin();
	end=--lst.end();
	lst.insert(i,start,end);
	cout << "lst.insert(i,start,end)" << endl;
	cout << "lst: ";
	for (i=lst.begin();i!=lst.end();i++)
		cout << *i << " ";
	cout << endl;
	return 1;
}

栈容器

代码语言:javascript
复制
//【例13.8】的程序:栈容器的使用
#include <iostream>
#include <stack>
using namespace std;
int main()
{	stack<int> st;
	st.push(1); st.push(2); st.push(3);    
	cout << st.top() << " ";
	st.pop();	cout << st.top() << " ";
	st.pop();	st.top() = 7;
	st.push(4);st.push(5);
	st.pop() ;
	while (!st.empty()) 	//栈不空时输出栈顶元素并退栈
	{	cout << st.top() << " ";
		st.pop() ;
	}
	cout << endl;
	return 1;
}

队列容器

代码语言:javascript
复制
//【例13.9】的程序:队列容器的使用
#include <iostream>
#include <queue>
using namespace std;
int main()
{	queue<int> q;
	q.push(1); q.push(2); q.push(3);
	cout << q.front() << " "; 
	q.pop(); cout << q.front() << " ";  q.pop();
	q.push(4); q.push(5);
	q.pop();
	while (!q.empty())   //队不空时出队
	{	cout << q.front() << " ";
		q.pop();
	}
	cout << endl;
	return 1;
}

废江博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 转载请注明原文链接:STL标准库容器

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-12-21),如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 向量容器
  • 双端队列容器
  • 链表容器
  • 栈容器
  • 队列容器
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档