使用 std::transform 和 tr1::bind 转换 std::complex 的向量
给定 std::complex 的 std::vector ,我想将其转换为仅包含复数实部的向量,除以某个常数系数。 现在,我这样做:
std::vector<std::complex<double> > vec;
std::vector<double> realVec;
double norm = 2.0;
...
for (std::vector<std::complex<double> >::iterator it = vec.begin(), itEnd = vec.end(); it != itEnd; ++it)
realVec.push_back((*it).real() / norm);
这当然很好,但我正在寻找一种使用 std::transform 来做同样事情的方法。我尝试过:
transform(vec.begin(), vec.end(), back_inserter(realVec), tr1::bind(divides<double>(), tr1::bind(&complex<double>::real, tr1::placeholders::_1), norm));
但行不通。我有这个错误:
erreur: no matching function for call to ‘bind(<unresolved overloaded function type>, std::tr1::_Placeholder<1>&)’|
我不明白为什么有“未解析的重载函数类型”。
有人可以向我解释一下出了什么问题吗?
Given a std::vector of std::complex, I would like to transform it to a vector containing only the real part of the complex, divided by some constant coefficient.
Right now, I do that:
std::vector<std::complex<double> > vec;
std::vector<double> realVec;
double norm = 2.0;
...
for (std::vector<std::complex<double> >::iterator it = vec.begin(), itEnd = vec.end(); it != itEnd; ++it)
realVec.push_back((*it).real() / norm);
This works fine of course, but I am looking for a way to use std::transform to do the same thing. I tried:
transform(vec.begin(), vec.end(), back_inserter(realVec), tr1::bind(divides<double>(), tr1::bind(&complex<double>::real, tr1::placeholders::_1), norm));
But it won't work. I have this error:
erreur: no matching function for call to ‘bind(<unresolved overloaded function type>, std::tr1::_Placeholder<1>&)’|
I don't understand why there is a "unresolved overloaded function type".
Could someone explain to me what is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,你不能这样做,至少不能直接这样做。标准库成员函数的类型(如
complex::real
)未指定,因此实现可能会提供额外的重载,并且那里的函数可能具有带有默认参数的附加参数。实际上,没有可移植的方法来获取标准库成员函数的地址。
你最好的选择是编写一个辅助函数:
并绑定到它:
Unfortunately, you can't do this, at least not directly. The types of Standard Library member functions (like
complex<double>::real
) are left unspecified, so an implementation may provide additional overloads and the functions that are there may have additional parameters with default arguments.In effect, there is no portable way to take the address of a Standard Library member function.
Your best bet would be to write a helper function:
and bind to that:
std::complex<>::real()
已重载(参见 C++11 [complex])。当您获取重载函数的地址时,C++ 需要消除歧义。就你而言,你必须说:
是的,这很丑陋。
std::complex<>::real()
is overloaded (cf. C++11 [complex]).C++ requires a disambiguation when you take the address of an overloaded function. In your case, you have to say:
Yes, this is ugly.
对于 C++11,您可以编写
You can simple by
using F = double (*)(const std::complex&)
并编写std::bind;(std::real,std::占位符::_1)
。从 C++14 开始,标准运算符模板的模板类型参数通常可以省略,因此您可以编写std::divides<>()
。For C++11, you can write
You can simplify by
using F = double (*)(const std::complex<double> &)
and writestd::bind<F>(std::real, std::placeholders::_1)
. Since C++14, the template type argument for the standard operator templates can generally be omitted, so you can writestd::divides<>()
.