C++ 向量中每个元素的平方和,用于
由于函数只接受一个参数(向量的元素),因此我必须在某个地方定义一个C++ 向量中每个元素的平方和,用于,c++,vector,stl-algorithm,C++,Vector,Stl Algorithm,由于函数只接受一个参数(向量的元素),因此我必须在某个地方定义一个静态int sum=0,以便可以访问它 在给每个人打电话之后。我觉得这很尴尬。有没有更好的方法(每个都使用) 请您给出更多示例,说明如何在实际编程中使用for_每个(而不仅仅是打印每个元素)?是否可以使用for_每个模拟“编程模式”,如map和injectin Ruby(或map/fold in Haskell) 编辑:谢谢大家。我从你的回复中学到了很多。在C++中,我们有很多方法来做同样的事情,这使得学习起来有点困难。但是很有趣
静态int sum=0
,以便可以访问它
在给每个人打电话之后。我觉得这很尴尬。有没有更好的方法(每个都使用)
请您给出更多示例,说明如何在实际编程中使用for_每个
(而不仅仅是打印每个元素)?是否可以使用for_每个
模拟“编程模式”,如map和injectin Ruby(或map/fold in Haskell)
编辑:谢谢大家。我从你的回复中学到了很多。在C++中,我们有很多方法来做同样的事情,这使得学习起来有点困难。但是很有趣:)使用
#包括
#包括
//获取上一个结果和当前元素平方和的函子
模板
结构广场
{
T运算符()(常数T和左、常数T和右)常数
{
返回(左+右*右);
}
};
void main()
{
std::向量v1;
v1.推回(1);
v1.推回(2);
v1.推回(3);
v1.推回(4);
intx=std::累加(v1.begin(),v1.end(),0,square());
//0代表每个元素依次与之组合的初始值
//因为我们的情况必须是0。
}
您可以像中那样模拟std::acculate,但我相信使用std::acculate将使代码更具可读性,因为它是为这样的目的而设计的。您可以找到更多的标准算法。
for\u每个
返回它所使用的函子(一个副本)。比如说:
#include <algorithm>
#include <vector>
#include <iostream>
template <typename T>
class square_accumulate
{
public:
square_accumulate(void) :
_sum(0)
{
}
const T& result(void) const
{
return _sum;
}
void operator()(const T& val)
{
_sum += val * val;
}
private:
T _sum;
};
int main(void)
{
int arr[] = {1,2,3,4};
std::vector<int> a (arr ,arr + sizeof(arr)/sizeof(arr[0]));
int sum = std::for_each(a.begin(), a.end(), square_accumulate<int>()).result();
std::cout << "sum of the square of the element is " << sum << std::endl;
}
#include是最好的方法。作为STL此类问题的一般解决方案:您可以传递一个functor
——例如,实现操作符()
的任何类的实例。这比依赖全局变量要好得多,因为所述实例可以保持和更新自己的状态!您可以将其视为一种“编译时鸭子类型”:泛型编程并不限制您在该位置传递“函数”,任何“行为类似于函数”(即,具有适当的运算符()
)的操作都可以 不要使用for_each()
为此,请使用
标题中的累积()
:
#include <numeric>
#include <iostream>
using namespace std;
struct accum_sum_of_squares {
// x contains the sum-of-squares so far, y is the next value.
int operator()(int x, int y) const {
return x + y * y;
}
};
int main(int argc, char **argv) {
int a[] = { 4, 5, 6, 7 };
int ssq = accumulate(a, a + sizeof a / sizeof a[0], 0, accum_sum_of_squares());
cout << ssq << endl;
return 0;
}
#包括
#包括
使用名称空间std;
结构累积平方和{
//x包含到目前为止的平方和,y是下一个值。
int运算符()(int x,int y)常量{
返回x+y*y;
}
};
int main(int argc,字符**argv){
int a[]={4,5,6,7};
int ssq=累加(a,a+a的大小/a的大小[0],0,平方的累计和();
不能否,不要使用std::accumulate()使用std::internal_product()。不需要函子
#include <vector>
#include <numeric>
void main()
{
std::vector <int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
int x = std::inner_product( v1.begin(), v1.end(), v1.begin(), 0 );
}
#包括
#包括
void main()
{
std::向量v1;
v1.推回(1);
v1.推回(2);
v1.推回(3);
v1.推回(4);
intx=std::内部乘积(v1.begin(),v1.end(),v1.begin(),0);
}
std::for_每个
用于对每个元素进行处理。如果您希望从所有元素的计算中获得结果,则有std::acculate
。如果您想要Haskell的映射
行为,请使用std::transform
您可以滥用这三种方法中的任何一种来做与其他方法相同的事情,因为它们最终只是在一个迭代器上进行迭代(除了以两个迭代器作为输入的transform
形式)重点是,<>代码>每个代码不是映射/折叠的替换-应该通过变换/累加来完成——虽然C++没有天生的表达映射/折叠概念的概念,但是Haskell确实如此——但是GCC和VC++都支持OpenMP,它在<>代码>
Ruby中的Inject与使用成熟的functor为每个
调用(如上所述)更接近。C++0X中带有变量捕获的Lambda函数将使两种语言之间的行为更加相似:
int main(void)
{
int arr[] = {1,2,3,4};
std::vector<int> a (arr ,arr + sizeof(arr)/sizeof(arr[0]));
int sum = 0;
std::for_each(a.begin(), a.end(), [&](int i) { sum += i*i;} );
std::cout << "sum of the square of the element is " << sum << std::endl;
}
int main(无效)
{
int arr[]={1,2,3,4};
标准::向量a(arr,arr+sizeof(arr)/sizeof(arr[0]);
整数和=0;
std::for_each(a.begin()、a.end()、[&](inti){sum+=i*i;});
std::cout这是std::accumulate仿真的一个很好的示例。对于教学目的很有用。向下投票评论?如果我不知道哪里出了问题,就无法改进。我不认为向量迭代器保证在std命名空间中。如果这是正确的,那么ADL就不能保证在这里工作,提问者也没有指定编译器。你是对的。我我刚刚检查过-标准并不保证迭代器是std命名空间的一部分。只有反向迭代器是std
命名空间的一部分。逐个:哇,好主意。我检查了标准,你完全正确。因此,例如,如果vector::iterator的类型定义为t*,ADL确实会失败。-1希望引起OP的注意。。。(这篇帖子很好,顺便说一句)好的,很快,不需要-1!:)非常优雅…正是我想要的!为什么这不是最好的答案?
#include <algorithm>
#include <vector>
#include <iostream>
template <typename T>
class square_accumulate
{
public:
square_accumulate(void) :
_sum(0)
{
}
const T& result(void) const
{
return _sum;
}
void operator()(const T& val)
{
_sum += val * val;
}
private:
T _sum;
};
int main(void)
{
int arr[] = {1,2,3,4};
std::vector<int> a (arr ,arr + sizeof(arr)/sizeof(arr[0]));
int sum = std::for_each(a.begin(), a.end(), square_accumulate<int>()).result();
std::cout << "sum of the square of the element is " << sum << std::endl;
}
#include <numeric>
#include <iostream>
using namespace std;
struct accum_sum_of_squares {
// x contains the sum-of-squares so far, y is the next value.
int operator()(int x, int y) const {
return x + y * y;
}
};
int main(int argc, char **argv) {
int a[] = { 4, 5, 6, 7 };
int ssq = accumulate(a, a + sizeof a / sizeof a[0], 0, accum_sum_of_squares());
cout << ssq << endl;
return 0;
}
#include <vector>
#include <numeric>
void main()
{
std::vector <int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
int x = std::inner_product( v1.begin(), v1.end(), v1.begin(), 0 );
}
int main(void)
{
int arr[] = {1,2,3,4};
std::vector<int> a (arr ,arr + sizeof(arr)/sizeof(arr[0]));
int sum = 0;
std::for_each(a.begin(), a.end(), [&](int i) { sum += i*i;} );
std::cout << "sum of the square of the element is " << sum << std::endl;
}