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;
}

图解


本篇完!

相关推荐
沐知全栈开发20 小时前
HTML5 浏览器支持
开发语言
wasp52020 小时前
AgentScope Java 核心架构深度解析
java·开发语言·人工智能·架构·agentscope
WHOVENLY20 小时前
【javaScript】- 笔试题合集(长期更新,建议收藏,目前已更新至31题)
开发语言·前端·javascript
慌糖20 小时前
流-为序列化解释
开发语言
LXS_35721 小时前
Day 18 C++提高 之 STL常用容器(string、vector、deque)
开发语言·c++·笔记·学习方法·改行学it
王琦031821 小时前
Python 函数详解
开发语言·python
胡伯来了21 小时前
13. Python打包工具- setuptools
开发语言·python
小鸡吃米…1 天前
Python 中的多层继承
开发语言·python
deng-c-f1 天前
Linux C/C++ 学习日记(53):原子操作(二):实现shared_ptr
开发语言·c++·学习
wanghowie1 天前
01.07 Java基础篇|函数式编程与语言新特性总览
java·开发语言·面试