STL容器——案例版

本文介绍了C++标准库中的几种重要容器,包括vector(动态数组)、stack(栈)、queue(队列)以及map(映射),展示了它们的基本使用方法,如初始化、插入、删除和遍历元素。示例代码详细演示了这些容器的功能,如vector的迭代器操作,stack的先进后出特性,queue的队列操作,以及map的键值对存储和访问。
摘要由CSDN通过智能技术生成

vector(动态数组,可变长数组)

/*
	vector --->向量  ---->线性容器
	用标准模板,记得加上相应的头文件
*/
#include <iostream>
#include <vector>
using namespace std;
int main()
{
	//向量容器
	vector <int> v0;
	//初始化值
	vector <int> v1{ 1,2,3,4,5,9,6 }; //设置向量容量
	//初始化
	v1[0] = 8;
	v1[1] = 8;
	v1[2] = 8;
	//声明迭代器 说明他属于那个模板
	vector<int>::iterator  v1_Iter;
	for (v1_Iter = v1.begin(); v1_Iter < v1.end(); v1_Iter++)
	{
		cout << " " << *v1_Iter;
	}
	cout << endl;
	//逆序迭代器
	vector<int>::reverse_iterator  v1_rIter;
	for (v1_rIter = v1.rbegin(); v1_rIter < v1.rend(); v1_rIter++)
	{
		cout << " " << *v1_rIter;
	}

	//新标准 --- 简洁的写法
	cout << endl;

	for (int i : v1)
	{
		cout << " " << i;
	}
	cout << endl;

	system("pause");
	return 0;
}

stack (栈)

#include <iostream>
#include<stack>
#include <string>
using namespace std;
int main()
{
	//stack 先进后出
	stack<string> mystack;
	mystack.push("饭");
	mystack.push("要吃");
	mystack.push("我");
	while (!mystack.empty())
	{
		cout << mystack.top();
		mystack.pop();
	}

	return 0;
}
//----------------------------------------- 读取堆栈的栈顶元素
#include <stack>
#include <iostream>
using namespace std;
int main()
{
    // 创建堆栈对象
    stack<int> s;
    // 元素入栈
    s.push(3);
    s.push(19);
    s.push(23);
    s.push(36);
    s.push(50);
    s.push(4);

    // 元素依次出栈
    while(!s.empty())
    {
        // 打印栈顶元素,打印出:4 50 36 23 19 3
        cout << s.top() << endl;
        // 出栈
        s.pop();
    }

    return 0;
}

queue(队列)

#include <iostream>
#include<queue>
#include <list>
#include <string>
using namespace std;
int main()
{
	queue<string> qs;

	qs.push("我");
	qs.push("想");
	qs.push("你");
	while (!qs.empty())
	{
		cout << qs.front();		//返回第一个元素
		qs.pop();				// 沒有pop会死循环。  //从队头移除第一个元素
	}
	
	return 0;
}


嵌套
//list<string> Ls;
//Ls.push_back("12123");
//queue<list<string>> QS;
//QS.push(Ls);
#include <queue>
#include <string>
#include<iostream>

using namespace std;
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	string m_Name;
	int m_Age;
};

void test01() {

	//创建队列
	queue<Person> q;

	//准备数据
	Person p1("唐僧", 30);
	Person p2("孙悟空", 1000);
	Person p3("猪八戒", 900);
	Person p4("沙僧", 800);

	//向队列中添加元素  入队操作
	q.push(p1);
	q.push(p2);
	q.push(p3);
	q.push(p4);

	//队列不提供迭代器,更不支持随机访问	
	while (!q.empty()) {
		//输出队头元素
		cout << "队头元素-- 姓名: " << q.front().m_Name
			<< " 年龄: " << q.front().m_Age << endl;

	/*	cout << "队尾元素-- 姓名: " << q.back().m_Name
			<< " 年龄: " << q.back().m_Age << endl;*/

		cout << endl;
		//弹出队头元素
		q.pop();
	}

	cout << "队列大小为:" << q.size() << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}


//push(elem); //往队尾添加元素
//pop(); //从队头移除第一个元素
//back(); //返回最后一个元素
//front(); //返回第一个元素
//
//empty(); //判断堆栈是否为空
//size(); //返回栈的大小
//
//queue& operator=(const queue& que); // 赋值操作--重载等号操作符

list(双向链表,列表)

#include<iostream>
#include <list>	
using namespace std;
int main()
{
	list<int>  L1;
 
	L1.push_back(3);
	L1.push_back(2);
	L1.push_back(1);
	for (int &v : L1)
	{
		cout << " " << v;
	}
	cout << endl;
	L1.push_front(4);
 
	list<int>::iterator  L1_Iter;
	//-----> p->next!= NULL
	for (L1_Iter = L1.begin(); L1_Iter!=L1.end(); L1_Iter++)
	{
		cout << " " << *L1_Iter;
	}
	system("pause"); //防止闪屏
	return 0;
}

/*  插入 list 链表元素 解释:
    push_back 函数在 list 尾部添加元素;
    push_front 函数在 list 链首插入元素;
    iterator insert(iterator pos, const T& x) 在任意位置插入元素
*/

#include <list>
#include <iostream>
using namespace std;
int main()
{
	list<int> lInt;
	lInt.push_back(1);
	lInt.push_back(3);
	lInt.push_back(4);
	lInt.push_back(5);
	// 插入链表元素
	list<int>::iterator i, iend;
	i = lInt.begin();
	++i;  // 因为是 链表结构,所以,只能通过指针的移位找到要插入的位置
	lInt.insert(i, 20);  // 在第2个位置,插入元素 20
	lInt.push_front(0);
	// 打印链表元素
	iend = lInt.end();
	for (i = lInt.begin(); i != iend; ++i)
		cout << *i << ' ';
	cout << endl;

	return 0;

}

set(集合,键和值相同)

特有find()

#pragma warning(disable:4786)
#include <set>
#include <iostream>
using namespace std;
int main()
{
    multiset<int> ms;
    ms.insert(10);
    ms.insert(13);
    ms.insert(11);
	ms.insert(19);
	ms.insert(13);
	ms.insert(19);
	ms.insert(19);
	// 打印数据
	multiset<int>::iterator i, iend;
	iend = ms.end();
	for (i = ms.begin(); i != iend; ++i)
		cout << *i << ' ';
	cout << endl;

    return 0;
}

map(单映射)

pair< frist(键),second(值) >
1、insert( pair<type,type> elem ) 插入键值
2、erase( iter ) 删除元素
3、size() 容器中元素的个数
4、begin()
5、end()

 #pragma warning(disable:4786)
 #include <map>
 #include <iostream>
 using namespace std;
 int main()
 {
	 // 创建 map 容器对象 m
	 map<const char*, float>  m;
	 // 插入元素(水果名,单价),水果名位键值,单价为映照数据
	 m["apple"] = 3.6f;
	 m["orange"] = 3.2f;
	 m["banana"] = 1.8f;
	 m["pear"] = 2.3f;
	 m["lichee"] = 6.3f;
	 m.insert(pair<const char*, float>("曾果粒", 100.001f));

	 // 打印元素
	 cout << "苹果价格:" << m["apple"] << "元/斤 \n";
	 cout << "桔子价格:" << m["orange"] << "元/斤 \n";
	 cout << "香蕉价格:" << m["banana"] << "元/斤 \n";
	 cout << "雪梨价格:" << m["pear"] << "元/斤 \n";
	 cout << "荔枝价格:" << m["lichee"] << "元/斤 \n";
	 cout << "pair插入  " << m["曾果粒"] << "元/斤\n";

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值