C++ 教程目录

函数探幽

arrtemp.cpp

来源: 函数探幽

点击上方按钮打开代码编辑器和可视化视图

⚠️ 交互式运行提示

为了防止网页挂起,所有 cin 输入语句已被自动注释。请直接在代码编辑器中修改变量的值来进行测试。

书本配套例题

原始代码预览

// arrtemp.cpp -- using template functions with array template
#include <iostream>
#include <array>
#include <string>
template <class T, size_t n>
void display(const std::array<T, n> & ar);
int main()
{
	std::array<int, 5> ai = {1,2,3,4,5}; //,6,7,8,9,22};
	std::array<std::string, 5> as =
	{
		"string under construction",
		"stupid string indeed",
		"there's nothing to see",
		"nothing to do",
		"but enjoy all that is"
	};
	display(ai);
	display(as);
	// std::cin.get();
	return 0;
}
template <class T, size_t n>
void display(const std::array<T,  n> & ar)
{
	for (int i = 0; i < 5; i++)
		std::cout << ar[i] << std::endl;
}
C++ Playground
运行结果 / 调试信息
等待编译...
本节课暂无动态演示