STL--vector容器的容器

容器的容器

容器中嵌套容器,我们将所有数据进行遍历输出

cpp 复制代码
vector<vector<int>>v; 

举个例子

cpp 复制代码
//容器嵌套容器
int main()
{
	vector<vector<int>>v; //容器里面放入容器
	vector<int>v1;
	vector<int>v2;
	vector<int>v3;
	vector<int>v4;

	for (int i = 0;i < 4;i++)
	{
		v1.push_back(i + 1);
		v2.push_back(i + 2);
		v3.push_back(i + 3);
		v4.push_back(i + 4);
	}
	//将容器元素插入到vector中
	v.push_back(v1);
	v.push_back(v2);
	v.push_back(v3);
	v.push_back(v4);


	//注意想要把容器里面的容器都输出出来,需要两个for循环
	for (auto it = v.begin();it != v.end();it++)
	{
		for (auto it2 = (*it).begin();it2 != (*it).end();it2++) {
			cout << *it2 << " ";
		}
		cout << endl;
	}
	return 0;
}

图解


本篇完!

相关推荐
秦苒&14 分钟前
【C语言指针二】从入门到通透:核心知识点全梳理(野指针,assert断言,指针的使用和传址调用,数组名的理解和使用指针反访问数组)
c语言·开发语言
qq_479875431 小时前
C++ 网络编程中的 Protobuf 消息分发 (Dispatcher) 设计模式
网络·c++·设计模式
Tandy12356_1 小时前
手写TCP/IP协议——IP层输出处理
c语言·网络·c++·tcp/ip·计算机网络
博语小屋1 小时前
实现简单日志
linux·服务器·数据库·c++
Y1rong4 小时前
C++ QT之记事本
开发语言·qt
ZouZou老师7 小时前
C++设计模式之装饰器模式:以家具生产为例
c++·设计模式·装饰器模式
ZouZou老师8 小时前
C++设计模式之桥接模式:以家具生产为例
c++·设计模式·桥接模式
diegoXie8 小时前
Python / R 向量顺序分割与跨步分割
开发语言·python·r语言
程序员小白条8 小时前
0经验如何找实习?
java·开发语言·数据结构·数据库·链表
呱呱巨基8 小时前
Linux 进程概念
linux·c++·笔记·学习