学习笔记:使用容器模板

参考书目:C/C++规范设计简明教程,P365

目的:联系STL模板的使用方法。

//使用标准类模板STL

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>	//是c++ 的头文件,用<string.h> 时,cout不能输出模板元素
#include <vector>
using namespace std;
typedef std::vector<string> CStrs;		//将string作为数组元素类型构造CStrs
typedef std::vector<CStrs> CDStrs;		//将CStrs作为数组元素类型构造CDStrs

int main()
{
	cout << "Hello World!\n";
	CStrs cstrs1;
	cstrs1.push_back("张三");
	cstrs1.push_back("男");
	cstrs1.push_back("合肥");

	CStrs cstrs2;
	cstrs2.push_back("董妹妹");
	cstrs2.push_back("女");
	cstrs2.push_back("北京");
	cstrs2.push_back("此人是博士");

	CDStrs cdstrs;
	cdstrs.push_back(cstrs1);
	cdstrs.push_back(cstrs2);

	for (int i = 0; i < cdstrs.size(); i++)
	{
		CStrs tempStrs;
		tempStrs = cdstrs[i];
		for (int j = 0; j < tempStrs.size(); j++)
		{
			std::cout << tempStrs[j] << "\t";
			//cout << "ssss" << endl;
		}
		cout << endl;
	}

	getchar();
}

运行结果如下:

发布了34 篇原创文章 · 获赞 1 · 访问量 730

猜你喜欢

转载自blog.csdn.net/qq_41708281/article/details/104173554