迭代器边界检查超出向量大小

Iterator bounds checking exceeding vector size

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


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

这是我的函数:

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() 它是否超过了向量的末尾。我该如何解决这个问题?

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?

您不需要两个不同的循环来遍历向量 values

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
}

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

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]);

另一方面,如果目标是从 连续元素对 的总和得到一个结果向量,则 temp1temp2 是多余的。 result可以简单填写:

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]);
}

因为你没有使用模板函数,你的函数只使用一个向量,我认为你不需要迭代器。

你可以像

...
for (int i = 0; i < values.size(); i += 2) {
    result.push_back(values[i] + values[i + 1]);
}
...

好吧,如果你确定 values.size() 总是偶数。 如果不是这种情况,您可以执行

void sumElementPairsFromVector(const std::vector<int>& values, std::vector<int>& result)
{
    for (int i = 1; i < values.size(); i += 2) {
        result.push_back(values[i] + values[i - 1]);
    }
    if (values.size() % 2) {
        // whatever you want to do with the last element of an uneven vector
        result.push_back(values[values.size() - 1]);
    }
}

因为如果您不确定值的大小是否始终为偶数,第一种方法将尝试访问第 n 个元素,其中 n 大于元素的数量,因此您可能会得到一个垃圾值。