【发布时间】:2015-10-19 04:07:45
【问题描述】:
我对 C++ 中的模板编程有点陌生,但遇到了困难。我正在尝试编写一段代码来循环容器和模板类型 T 中的元素。我所拥有的显然是错误的,但我认为应该理解这个想法。
template <typename Container, typename T >
Container<T> MyFunction(Container<T> input)
{
T precedingElement = input[0];
Container<T> output = input;
for(int i=1; i<input.size(); i++)
{
// Do some work on element in the container
// Now update precedingElement
precedingElement = input[i];
}
return output;
}
// Example
vector<float> a = [0, 1, 2, 3, 4, 5 ...];
vector<float> b = MyFunction(a);
// Another example
list<MyType> c = [object1, object2, ... ];
list<MyType> d = MyFunction(c)
提前感谢您的帮助。
【问题讨论】:
标签: c++ metaprogramming template-meta-programming