C++ 将常量值添加到 std::vector 的 STL 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4461446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
STL way to add a constant value to a std::vector
提问by Hans
Is there an algorithm in the standard library that can add a value to each element of a std::vector? Something like
标准库中是否有一种算法可以为 std::vector 的每个元素添加一个值?就像是
std::vector<double> myvec(5,0.);
std::add_constant(myvec.begin(), myvec.end(), 1.);
that adds the value 1.0 to each element?
将值 1.0 添加到每个元素?
If there isn't a nice (e.g. short, beautiful, easy to read) way to do this in STL, how about boost?
如果在 STL 中没有一种很好的(例如简短、美观、易于阅读)的方法来做到这一点,那么 boost 怎么样?
回答by Gene Bushuyev
Even shorter using lambda functions, if you use C++0x:
如果使用 C++0x,使用 lambda 函数甚至更短:
std::for_each(myvec.begin(), myvec.end(), [](double& d) { d+=1.0;});
回答by Michael Kristofik
Take a look at std::for_each
and std::transform
. The latter accepts three iterators (the begin and end of a sequence, and the start of the output sequence) and a function object. There are a couple of ways to write this. One way, using nothing but standard stuff, is:
看看std::for_each
和std::transform
。后者接受三个迭代器(序列的开始和结束,以及输出序列的开始)和一个函数对象。有几种方法可以写这个。一种方法,只使用标准的东西,是:
transform(myvec.begin(), myvec.end(), myvec.begin(),
bind2nd(std::plus<double>(), 1.0));
You can do it with for_each
as well, but the default behavior of std::plus
won't write the answer back to the original vector. In that case you have to write your own functor. Simple example follows:
您也可以这样做for_each
,但默认行为std::plus
不会将答案写回原始向量。在这种情况下,您必须编写自己的函子。简单的例子如下:
struct AddVal
{
double val;
AddVal(double v) : val(v);
void operator()(double &elem) const
{
elem += v;
}
};
std::for_each(myvec.begin(), myvec.end(), AddVal(1.0));
回答by Thomas Petit
The shortest way in plain C++0X is :
普通 C++0X 中最短的方法是:
for(double& d : myvec)
d += 1.0;
and with boost :
和提升:
for_each(myvec, _1 += 1.0); // boost.range + boost.lambda
回答by ltjax
std::transform( myvec.begin(), myvec.end(),
myvec.begin(), std::bind2nd( std::plus<double>(), 1.0 ) );
回答by André Caron
If you're interested in performing lots of math on vectors, you might want to look into the <valarray>
part of the standard library. It's basically a std::vector<>
designed for vectorial numerical computations (slices, math functions, point-wise operators, etc.).
如果您有兴趣对向量执行大量数学运算,您可能需要查看<valarray>
标准库的一部分。它基本上是std::vector<>
为矢量数值计算(切片、数学函数、逐点运算符等)设计的。
The only part that really sucks is it doesn't have easy conversions to and from std::vector<>
.
唯一真正糟糕的部分是它与std::vector<>
.
回答by Spandyie
The best way is to add std::transform()
and lambda expression.
最好的方法是添加std::transform()
和 lambda 表达式。
#include <vector>
#include <algorithm>
std::vector<double> myVector;
double constantElement;
std::transform<myvector.begin(), myVector.end(), myVector.begin(),[&](auto& value){ return value+constantElement;});