【发布时间】:2017-06-15 03:57:05
【问题描述】:
我在迭代可能具有任意数量数据结构(列表、向量等)的模板时遇到问题
对于给定的模板,是否有一种万无一失的方法来遍历它?我在声明类型时遇到了麻烦,是的,我尝试了“auto”和“auto&”,它们似乎根本不起作用。谢谢指点。
template<typename ElementType, typename ContainerType>
ElementType findMax(ContainerType& container) {
for (typeid(container).name i = container.begin(); i !=container.end(); ++i){
cout << i;
}
return 10;
}
【问题讨论】:
-
你有什么问题?
auto有什么问题?什么根本不起作用?为什么需要type? -
为什么 'auto' 不适合你?
-
是的。
auto直到 C++11 才被添加 -
.name 只是一个字符串!
-
为什么不直接使用调用代码中的
std::max_element让整个函数变得毫无意义?