为什么这个基于范围的for循环会导致减法问题?

我遇到了基于范围的 for 循环的问题,导致我的值变为负数,我已经使用常规 for 循环解决了该问题,但想了解它为什么一开始就搞砸了。您可以从下面的示例输出中看到初始值正确存在,但是当尝试从它们中减去时,它们被重置为默认的初始化值 0 我猜?

损坏的代码:

#include <iostream>
#include <vector> 

#define IS_TRUE(x) { if (!(x)) std::cout << __FUNCTION__ << " failed on line " << __LINE__ << std::endl; }

int maximumScore(std::vector<int>& nums, std::vector<int>& multipliers) {

    std::vector<int> multRank;
    multRank.resize(multipliers.size());
    
    std::cout << "multRank: ";
//ISSUE IS IN THE LOOP BELOW
    for (int n : multRank) {
        n = multipliers.size();
        std::cout << " " << n;
    }
    std::cout << std::endl;
    for (auto i = 0; i < multipliers.size(); ++i) {
        for (auto j = 0; j < multipliers.size(); ++j) {
            int abs1 = std::abs(multipliers[i]);
            int abs2 = std::abs(multipliers[j]);
            if (abs1 > abs2) {
                multRank[i] = multRank[i] - 1;
                std::cout << multRank[i];
            }
        }
    }
    std::cout << std::endl << "multRank after: ";
    for (int n : multRank) {
        std::cout << " " << n;
    }
    std::cout << std::endl << std::endl;

    return 0;
}


void test1()
{
    std::vector<int> nums = { 1, 2, 3 };
    std::vector<int> multipliers = { 3, 2, 1 };

    int test = maximumScore(nums, multipliers);

    IS_TRUE(test == 14);
}




int main()
{
    std::cout << "Maximum Score from Performing Multiplication Operationsn";

    test1();
}

损坏的代码输出:

Maximum Score from Performing Multiplication Operations
multRank:  3 3 3
-1-2-1
multRank after:  -2 -1 0

修复代码:

#include <iostream>
#include <vector> 

#define IS_TRUE(x) { if (!(x)) std::cout << __FUNCTION__ << " failed on line " << __LINE__ << std::endl; }

int maximumScore(std::vector<int>& nums, std::vector<int>& multipliers) {
    std::vector<int> multRank;
    multRank.resize(multipliers.size());
    
    std::cout << "multRank: ";
//ISSUE WAS IN THE LOOP BELOW
    for (auto i = 0; i < multipliers.size(); ++i) {
        multRank[i] = multipliers.size();
        std::cout << " " << multRank[i];
    }
    std::cout << std::endl;
    for (auto i = 0; i < multipliers.size(); ++i) {
        for (auto j = 0; j < multipliers.size(); ++j) {
            int abs1 = std::abs(multipliers[i]);
            int abs2 = std::abs(multipliers[j]);
            if (abs1 > abs2) {
                 multRank[i]--;
                std::cout << multRank[i];
            }
        }
    }
    std::cout << std::endl << "multRank after: ";
    for (int n : multRank) {
        std::cout << " " << n;
    }
    std::cout << std::endl << std::endl;

    return 0;
}



void test1()
{
    std::vector<int> nums = { 1, 2, 3 };
    std::vector<int> multipliers = { 3, 2, 1 };

    int test = maximumScore(nums, multipliers);

    IS_TRUE(test == 14);
}


int main()
{
    std::cout << "Calculate Rankn";

    test1();
}

修复代码输出:

Maximum Score from Performing Multiplication Operations
multRank:  3 3 3
212
multRank after:  1 2 3

回答

基于 for 循环的第一个范围不使用引用:

for (int n : multRank) {
    n = multipliers.size();
    std::cout << " " << n;
}

在这个循环中,n是 中数据的副本multRank。如果你想能够修改中的数据multRank,你想n成为一个参考:

for (int& n : multRank) {
    n = multipliers.size();
    std::cout << " " << n;
}


以上是为什么这个基于范围的for循环会导致减法问题?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>