c++ - 迭代器边界检查超出 vector 大小

标签 c++ iterator c++17 indexoutofboundsexception stdvector

我的问题有所不同,因为我不是在寻找 range-3v 解决方案。另外,我特别询问如何解决第二个循环的问题。我已经接受了我的问题下面的答案。因为我不需要第二个 for 循环,他们向我展示了如何通过与 1 进行奇数迭代来使用单个索引。这解决了我的问题!


我正在编写一个函数,该函数将采用一个 vector ,假设它的元素长度为偶数;在我的函数中,我从原始 vector 创建两个临时 vector ,其元素为 {0,2,4,6,...}{1,3,5, 7,...} 分别。然后我添加相应的索引元素并将结果存储到我的结果 vector 中。

这是我的功能:

void sumElementPairsFromVector(const std::vector<int>& values, std::vector<int>& result)
{
    using It = std::vector<int>::const_iterator;
    std::vector<int> temp1, temp2;

    // First lets divide the original vector into two temp vectors
    for (It it1 = values.cbegin(); it1 != values.cend(); it1 += 2)
        temp1.push_back(*it1);

    for (It it2 = values.cbegin() + 1; it2 != values.cend() ; it2 += 2)
        temp2.push_back(*it2);

    // Add each corresponding vector and store that into our results.
    for (std::size_t i = 0; i < values.size() / 2; i++)
        result[i] = temp1[i] + temp2[i];
}

这是我的使用方法:

int main() 
{         
    std::vector<int> values{ 1,2,3,4,5,6 };
    for (auto i : values)
        std::cout << i << " ";
    std::cout << '\n';

    std::vector<int> results;

    sumElementPairsFromVector(values, results);
    for (auto i : results)
        std::cout << i << " ";
    std::cout << '\n';

    return 0;
}

预期输出应该是:

1 2 3 4 5 6
3 7 11

调试断言在函数的这行代码上失败:

for (It it2 = values.cbegin() + 1; it2 != values.cend(); it2 += 2 )

我知道是什么原因导致了错误;在最后一次迭代中,它增加 2 并检查 it2 !=values.cend() 是否超出了 vector 的末尾。我该如何解决这个问题?

最佳答案

I know what is causing the error; on the last iteration after it increments by 2 and goes to check if it2 != values.cend() it is going past the end of the vector. How do I fix this?

您不需要两个不同的循环来迭代 vector

std::vector<int> temp1, temp2;
temp1.reserve(values.size() / 2); // reserve the memory
temp2.reserve(values.size() / 2);

for (std::size_t index = 0; index < values.size(); ++index)
{
    if (index & 1) temp2.emplace_back(values[index]); // odd index
    else temp1.emplace_back(values[index]);           // even index
}

其次,结果当时没有分配任何内存

result[i] = temp1[i] + temp2[i];

因此 out of bound undefined behavior 。你应该

for (std::size_t i = 0; i < std::min(temp1.size(), temp2.size()); i++)
    result.emplace_back(temp1[i] + temp2[i]);

另一方面,如果目标是从连续元素对的总和中获得结果 vector ,则 temp1temp2是多余的。 结果可以简单地填写:

void sumElementPairsFromVector(const std::vector<int>& values, std::vector<int>& result)
{
    result.reserve(values.size() / 2);

    for (std::size_t index = 0; index < values.size() - 1; index += 2)
        result.emplace_back(values[index] + values[index+1]);
}

关于c++ - 迭代器边界检查超出 vector 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56407729/

相关文章:

C++ 重载模式 : call resolution with mutable lambda

c++ - 指定具有特征的模板化类

c++ - 使用模板类专门化模板函数

c++ - 模板参数推导顺序

c++ - boost::flat_set 的迭代器无法合并

c++ - 将折叠表达式传递给可变参数模板

c++ - 必须在 std::vector<std::thread> 中加入 std::thread 两次以避免从线程 dtor 终止

c++ - 为什么eclipse总是出现错误 "An internal error occurred during: "Notifying selection listeners.java.lang.StackOverflowError”

java - linklist.iterator().next() 如何工作?

algorithm - 有效地排序排列