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

图解


本篇完!

相关推荐
苏比的博客11 分钟前
Windows MFC添加类,变量,类导向
c++·windows·mfc
yudiandian201418 分钟前
MFC - 使用 Base64 对图片进行加密解密
c++·mfc
yudiandian201419 分钟前
MFC - Picture Control 控件显示图片
c++·mfc
QX_hao5 小时前
【Go】--反射(reflect)的使用
开发语言·后端·golang
inferno5 小时前
Maven基础(二)
java·开发语言·maven
我是李武涯5 小时前
从`std::mutex`到`std::lock_guard`与`std::unique_lock`的演进之路
开发语言·c++
卡提西亚6 小时前
C++笔记-10-循环语句
c++·笔记·算法
史不了6 小时前
静态交叉编译rust程序
开发语言·后端·rust
亮剑20186 小时前
第1节:C语言初体验——环境、结构与基本数据类型
c++
读研的武7 小时前
DashGo零基础入门 纯Python的管理系统搭建
开发语言·python