首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >std::accumulate的迭代器

std::accumulate的迭代器
EN

Stack Overflow用户
提问于 2019-07-06 17:40:55
回答 1查看 117关注 0票数 1

错误似乎与std:: accumulate()或迭代器有关,或者我正在访问无效的指针?

代码语言:javascript
复制
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)  );
    }
}

我试过上面的代码,但程序意外停止。

EN

回答 1

Stack Overflow用户

发布于 2019-07-06 17:46:34

问题是j可以小于i。这个版本可以工作

代码语言:javascript
复制
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)  );
    }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56912901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档