0
我有一个矢量,我想用标量值乘以它的每个元素。我写了下面的代码,这使用STL算法的矢量操作
int func(const int x)
{
return 10 * x; // say the scalar value is 10
}
void foo()
{
// .. compute vector vert
std::transform(vert.begin(), vert.end(), vert.begin(), ::func);
}
的代码做这项工作,但我想知道如果有一个更简洁的方式可能对于这一点,一个不需要我来定义FUNC功能seperately
[Lambda是你的朋友。](http://coliru.stacked-crooked.com/a/8b2b8cb813ce2114) – 0x499602D2 2014-11-04 23:38:42
它是否必须使用算法? 'for(int&x:vert)x * = 10;' – 2014-11-04 23:41:48
@MikeSeymour:或甚至用C++ 1z:'for(x:vert)x * = 10;' – Deduplicator 2014-11-04 23:42:49