[英]Is there something like std::accumulate that operates on iterators?
[英]iterators for std::accumulate
该错误似乎与std:: accumulate()
或迭代器有关,还是我正在访问无效的指针?
int m = 0;
std::vector<int> v{4,-3,0,-5};
for(std::vector<int>::iterator i = v.begin(); i != v.end(); i++)
{
for(std::vector<int>::iterator j = v.begin(); j != v.end(); j++)
{
m = max( m, std::accumulate(i, j, 0) );
}
}
我已经尝试了上面的代码,但是程序意外停止。
问题是j
可以小于i
。 此版本有效
int m = 0;
std::vector<int> v{4,-3,0,-5};
for(std::vector<int>::iterator i = v.begin(); i!=v.end(); i++)
{
for(std::vector<int>::iterator j = i; j!=v.end(); j++)
// ^^^^^^
{
m = max( m, std::accumulate(i, j, 0) );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.