I am trying to use std::for_each to output the contents of vectors, which may contain different types. So I wrote a generic output function like so:
我正在尝试使用std :: for_each来输出向量的内容,这些向量可能包含不同的类型。所以我写了一个通用输出函数,如下所示:
template<typename T> void output(const T& val)
{
cout << val << endl;
}
which I would like to use with:
我想用它:
std::for_each(vec_out.begin(), vec_out.end(), output);
but the compiler complains with "could not deduce template argument" in the for_each statement. Also complains with "A function template cannot be an argument to another function template".
但是编译器在for_each语句中抱怨“无法推断出模板参数”。还抱怨“函数模板不能成为另一个函数模板的参数”。
Is this not possible? I would have thought the compiler would know the type of vec_out (it's vector) and so should instantiate the function "output(const double& val)"?
这不可能吗?我原以为编译器会知道vec_out的类型(它的向量),所以应该实例化函数“output(const double&val)”?
If this doesn't work how can I get similar STL functionality without writing manual loops?
如果这不起作用,如何在不编写手动循环的情况下获得类似的STL功能?
I am quite new to C++ and still learning the ropes :-)
我对C ++很新,还在学习绳索:-)
3 个解决方案
#1
Try:
std::for_each(vec_out.begin(), vec_out.end(), output<T>);
where vec_out
is a container (vector
) of type T.
其中vec_out是T类型的容器(向量)。
Note: The for_each
algorithm expects an unary functor for its last argument. See the link for an example using functors.
注意:for_each算法需要一个一元仿函数用于其最后一个参数。有关使用仿函数的示例,请参阅链接。
#2
You have to pass an instantiation of the template. Something like output<int>
if your vector is vector of integers.
您必须传递模板的实例化。如果你的向量是整数向量,那就像输出
For example:
template<typename T> void output(const T& val)
{
cout << val << endl;
}
void main(int argc,char *argv[])
{
std::vector<int> vec_out;
std::for_each(vec_out.begin(), vec_out.end(), output<int>);
}
#3
I'd just like to add to the correct answers: the compiler can deduce the type if you wrap up your template function in a function object (aka functor):
我只想添加正确的答案:如果你将模板函数包装在一个函数对象(aka functor)中,编译器可以推断出类型:
struct OutputFunctor
{
template <typename T>
void operator()(const T& val) const { output(val); }
};
void test()
{
std::vector<int> ints;
std::vector<float> floats;
std::for_each(ints.begin(), ints.end(), OutputFunctor());
std::for_each(floats.begin(), floats.end(), OutputFunctor());
}
#1
Try:
std::for_each(vec_out.begin(), vec_out.end(), output<T>);
where vec_out
is a container (vector
) of type T.
其中vec_out是T类型的容器(向量)。
Note: The for_each
algorithm expects an unary functor for its last argument. See the link for an example using functors.
注意:for_each算法需要一个一元仿函数用于其最后一个参数。有关使用仿函数的示例,请参阅链接。
#2
You have to pass an instantiation of the template. Something like output<int>
if your vector is vector of integers.
您必须传递模板的实例化。如果你的向量是整数向量,那就像输出
For example:
template<typename T> void output(const T& val)
{
cout << val << endl;
}
void main(int argc,char *argv[])
{
std::vector<int> vec_out;
std::for_each(vec_out.begin(), vec_out.end(), output<int>);
}
#3
I'd just like to add to the correct answers: the compiler can deduce the type if you wrap up your template function in a function object (aka functor):
我只想添加正确的答案:如果你将模板函数包装在一个函数对象(aka functor)中,编译器可以推断出类型:
struct OutputFunctor
{
template <typename T>
void operator()(const T& val) const { output(val); }
};
void test()
{
std::vector<int> ints;
std::vector<float> floats;
std::for_each(ints.begin(), ints.end(), OutputFunctor());
std::for_each(floats.begin(), floats.end(), OutputFunctor());
}