我正在尝试使用 std::max_element 查找向量的最大值。当我运行程序时,出现分段错误,我认为这与 std::end 超出向量的末尾有关?我尝试将其更改为 std::end(tempdata)-1) 但无济于事。
auto max = std::max_element(std::begin(tempdata), std::end(tempdata));
std::ofstream maxcurrent("maxcurrent.txt", std::ios::app);
maxcurrent << v << std::setw(15) << *max << std::endl;
看过之后这个答案 https://stackoverflow.com/a/10159170我不明白为什么我的不工作。
当max
is std::end(tempdata)
如果您的tempdata
是空的。
指向范围 [first, last) 中最大元素的迭代器。如果范围中的多个元素等于最大元素,则将迭代器返回到第一个这样的元素。如果范围为空,则返回最后一个。
source http://en.cppreference.com/w/cpp/algorithm/max_element
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)