利用typename使用模板类型参数来定义模板类型中的类型的变量

版权声明:本文为博主原创文章,如有需要,可以随意转载,请注明出处。 https://blog.csdn.net/xunye_dream/article/details/82930807

直接上代码。

#include <iostream>

template<typename T>
void PrintStlContainer(T const& coll)
{
	typename T::const_iterator pos;                 //要使用模板参数中定义得类型,必须使用typename
	typename T::const_iterator end(coll.end());

	for (pos = coll.begin(); pos != end; pos++)
	{
		std::cout << *pos << " ";
	}

	std::cout << std::endl;
}

测试代码:

#include "inc.hpp"
#include <vector>

using namespace std;

int main(int argc, char **argv)
{
	vector<int> list{1, 2, 3, 4, 5};

	PrintStlContainer(list);

	return 0;
}

输出结果:

1 2 3 4 5 

猜你喜欢

转载自blog.csdn.net/xunye_dream/article/details/82930807
今日推荐