当前位置: 首页 > news >正文

【STL中容器汇总】map、list、vector等详解

容器学习分享

  • 1、STL简介
    • 1.1、STL六大组件
  • 2、vector容器
    • 2.1、vector 基本操作
    • 2.2、vector容器示例
    • 2.3、vector容器存放自定义数据类型示例
    • 2.3、vector嵌套vector示例
  • 3、list 容器
    • 3.1使用示例
    • 3.2、list容器基本函数
  • 4、map容器
    • 4.1、map函数原型
    • 4.2、map函数示例

1、STL简介

STL,标准模板库,是C++标准库的一部分,重在提高了代码的复用性;主要包含了常用的数据结构和基本算法,为广大c++程序员提供了一个可扩展的应用框架。

  • STL(Standard Template Library,标准模版库)
  • STL从广义上分为:容器(container)算法(algorithm)迭代器(iterator)
  • 容器和算法之间通过迭代器进行无缝连接。
  • STL几乎所有的代码都采用了模版类或模版函数

1.1、STL六大组件

STL大体分为六大组件,分别是:容器,算法,迭代器,适配器,函数对象,分配器。

  • 容器:各种数据结构,如vector,list,map,deque,set等,用来存放数据。
  • 算法:提供了一组常见的算法,如排序、查找、拷贝、变换等。
  • 迭代器:用于遍历和访问容器中的元素。
  • 函数对象:是一种可调用对象,可以像函数一样使用。
  • 适配器:是一种设计模式,用于将某个类转换为另一个类的接口。
  • 分配器:是用于管理内存分配的组件。它们通常用于动态分配内存,并将分配的内存返回给调用者。

2、vector容器

2.1、vector 基本操作

(1)头文件
#include.
(2)创建vector对象,
vector vec;
(3)尾部插入数字:
vec.push_back(a);
(4)使用下标访问元素
cout<<vec[0]<<endl;记住下标是从0开始的。
(5)使用迭代器访问元素.
vector::iterator it;
for(it=vec.begin();it!=vec.end();it++)
cout<<*it<<endl;
(6)插入元素:
vec.insert(vec.begin()+i,a);在第i+1个元素前面插入a;
(7)删除元素:
vec.erase(vec.begin()+2);删除第3个元素
vec.erase(vec.begin()+i,vec.end()+j);删除区间[i,j-1];区间从0开始
(8)向量大小:
vec.size();
vec.resize;改变大小
(9)清空:
vec.clear();

2.2、vector容器示例

#include <iostream>
#include <algorithm> //标准算法头文件
#include <vector>	//包含向量容器头文件
using namespace std ;void myPrint(int val)
{cout<< val <<endl;
}
void test01()
{//创建迭代器vector<int> v;//向容器中添加数据v.push_back(10);v.push_back(20);v.push_back(30);//通过迭代器遍历读取容器中的数据vector<int>::iterator it;for(it=v.begin();it!=v.end();it++){cout<<*it<<endl;}//另一种遍历方式for_each(v.begin(),v.end(),myPrint);
}
int  main()
{    test01();return 0;
}

2.3、vector容器存放自定义数据类型示例

#include <iostream>
#include <vector>using namespace std;class Person
{
public:Person(string name,int age){this->name =name;this->age=age;}string name;int age;
};
void test01()
{vector<Person> v;Person p1("aaa",10);Person p2("bbb",20);Person p3("ccc",30);//向容器中添加数据v.push_back(p1);v.push_back(p2);v.push_back(p3);//遍历容器vector<Person>::iterator it;for(it=v.begin();it!=v.end();v++{cout<<(*it).nema<<endl;cout<<(*it).age<<endl;}
}
void test02()
{vector<Person*> v;Person p1("aaa",10);Person p2("bbb",20);Person p3("ccc",30);//向容器中添加数据v.push_back(&p1);v.push_back(&p2);v.push_back(&p3);//遍历容器vector<Person*>::iterator it;for(it=v.begin();it!=v.end();v++{cout<<(*it)->nema<<endl;cout<<(*it)->age<<endl;}
}
int main()
{test01();test02();return 0;
}

2.3、vector嵌套vector示例

#include <iostream>
#include <vector>using namespace std;void test01()
{vector<vector<int>> v;vector<int> v1;vector<int> v2;vector<int> v3;for(int i=0;i<4;i++){v1.push_back(i+1);v2.push_back(i+2);v3.push_back(i+3);}v.push_back(v1);v.push_back(v2);v.push_back(v3);vector<vector<int>>::iterator vs;for(vs=v.begin();vs!=v.end();vs++){//(*vs)  --容器 vector<int>vector<int>::iterator vit;for(vit=(*vs).begin();vit!=(*vs).end();vit++){cout<<*vit<<endl;}}}
int main()
{test01();return 0;
}

3、list 容器

功能:将数据进行链式存储
链表(list)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链表实现的。
链表的组成:链表是由一系列结点组成
结点的组成:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域;

STL中的链表是一个双向循环链表
在这里插入图片描述

3.1使用示例

#include <iostream>
#include <list>
using namespace std ;
int main(){list<int> Link;	//构造一个列表用于存放整数链表int i, key, item;    for(i=0;i < 10;i++)// 输入10个整数依次向表头插入{cin>>item;Link.push_front(item);}cout<<“List:; // 输出链表list<int>::iterator p=Link.begin();while(p!=Link.end()){ //输出各节点数据,直到链表尾cout <<*p << "  ";p++;  //使P指向下一个节点}cout << endl;cout << "请输入一个需要删除的整数: ";cin >> key;Link.remove(key);   cout << "List: "; // 输出链表p=Link.begin();	// 使P重新指向表头while(p!=Link.end()){ cout <<*p << "  ";p++; // 使P指向下一个节点}cout << endl;
}

3.2、list容器基本函数

在这里插入图片描述

4、map容器

map容器中所有元素都是pair
pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
所有元素都会根据元素的键值自动排序

4.1、map函数原型

在这里插入图片描述

4.2、map函数示例

#include <iostream>
#include <map>
using namespace std;void test()
{	map<int ,int> m;	//创建map容器map<int ,int>::iterator it;//创建map容器迭代器//容器插入方式//一m.insert(pair<int,int>(1,10));//二m.insert(make_pair(2,20));//三m.insert(map<int,int>::value.type(3,30));//四m[4]=40;//容器的删除m.erase(m.begin());   //删除容器中首位元素m.erase(3);			  //删除容器中key为3的元素//查找map<int,int>::iterator pos =m.find(3);if(pos!=m.end()){cout<<"key"<<it->first<<"value"<<it->second<<endl;}}
int main()
{test();return 0;
}
http://www.lryc.cn/news/433840.html

相关文章:

  • Semantic Kernel + Natasha:一小时快速生成100个API的奇迹
  • rancher upgrade 【rancher 升级】
  • 【Linux】多线程:线程互斥、互斥锁、线程安全
  • 进程之间的通信方式
  • 动手学深度学习(pytorch)学习记录26-卷积神经网路(LeNet)[学习记录]
  • log4j 和 java.lang.OutOfMemoryError PermGen space
  • 2024.9.9营养小题【2】
  • uniapp的barcode组件去掉自动放大功能
  • H5接入Steam 获取用户数据案例
  • 《A Few Useful Things to Know about Machine Learning》论文导读
  • 隔壁老樊2024全国巡回演唱会重磅来袭,首站广州正式官宣!
  • 【C++】list(下)
  • 千云物流 -低代码平台MySQL备份数据
  • MySQL:进阶巩固-视图
  • 分布式事务Seata原理及其项目使用
  • JS_分支结构
  • 决策树(Decison Tree)—有监督学习方法、概率模型、生成模型、非线性模型、非参数化模型、批量学习
  • java 自定义注解校验实体类属性
  • 光伏并网发电系统中电能质量监测与优化技术探讨
  • 网页解析的那些事
  • 从文字到世界:2024外语阅读大赛报名开启,赛氪网全程护航
  • 微信小程序知识点(二)
  • Springcould -第一个Eureka应用 --- day02
  • RedissonClient 分布式队列工具类
  • protobuf使用
  • 【微处理器系统原理与应用设计第十二讲】通用定时器设计二之PWM波实现呼吸灯的功能
  • 2025秋招NLP算法面试真题(十九)-大模型分布式训练题目
  • 线程池的应用
  • OPenCV结构分析与形状描述符(5)查找图像中的连通组件的函数connectedComponents()的使用
  • HCIA--实验十三:VLAN间通信子接口实验/双单臂路由实验