简体   繁体   English

将数组的值传递给修改值的函数,然后将其返回到新数组中

[英]Passing values of array to a function that modifies the value then returns it into a new array

I'm having trouble getting my code to return the correct arrays. 我在获取代码以返回正确的数组时遇到麻烦。 void map takes in a function such as tiple and modifies the value that was passed into it from the array src and then returns the new value into dst which is then printed out. void map接受诸如tiple之类的函数,并修改从数组src传递给它的值,然后将新值返回到dst,然后将其打印出来。 I can get the code to compile but it doesn't return the correct array. 我可以编译代码,但是没有返回正确的数组。 For example, when it takes in [1,2,3,4] it returns [0,3,6,9] instead of [3,6,9,12] 例如,当它接受[1,2,3,4]时,它返回[0,3,6,9]而不是[3,6,9,12]

typedef int (*intModifier)(int);

int triple(int x){
    return 3*x;
}

void map(intModifier func, int src[], int dst[], int length){
    for(int *i = src; i < src + length; ++i){
    dst[*i] = func(*i);
    }
return;

}

void printIntArray(int arr[], int length){
cout << "{";
    if (length > 0){
    cout << arr[0];
    }
    for(int i = 1; i < length; ++i){
    cout << ", " << arr[i];
    }
    cout << "}";
    }

int main(){

int arr1[4] = {1, 2, 3, 4};
int arr2[4] = {0, 0, 0, 0};
int arr3[4] = {0, 0, 0, 0};
int arr4[4] = {0, 0, 0, 0};

cout << "Testing map." << endl;
cout << "  setting arr1 = {1,2,3,4}" << endl << endl;

cout << "  mapping from arr1 to arr2 using triple" << endl;
map(triple, arr1, arr2, 4);
cout << "  arr2 = ";
printIntArray(arr2, 4);  cout << endl << endl;
return 0;
}

Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

I am not sure why it is only the second element being modified. 我不确定为什么它只是第二个要修改的元素。 But the fact that is only one is almost definitely this: 但是几乎只有一个事实是这样的:

void map(intModifier func, int src[], int dst[], int length){
    for(int *i = src; i < src + length; i++){
        dst[*i] = func(src[*i]);
        return; // You return here!!!
    }
}

You will always return from map during the first iteration of your for loop. for循环的第一次迭代中,您将始终从map返回。 See how beneficial it is to indent your code! 看看缩进代码有多大好处!

Your map function is what is causing most of the issues. 导致大多数问题的是map功能。 Especially in the way you are trying to conduct the operations in the for loop. 尤其是以您尝试在for循环中进行操作的方式。

void map(intModifier func, int src[], int dst[], int length){
    for(int *i = src; i < src + length; ++i){ // There are some major issues in how you are 
        dst[*i] = func(*i);                   // iterating through the source array.
    }
return;

}

You are using the value of the source array to increment and store data between your destination array and source array. 您正在使用源数组的值在目标数组和源数组之间递增和存储数据。 Your arrays are only 4 int "blocks" yet you increment through source at the source base length plus 4 i < src + length which is going well past your array length. 您的数组只有4个int“块”,但是您以源基本长度加4 i < src + length (远远超出数组长度)在源中递增。

It would be better if you iterate through using the length you pass in, that's essentially why you pass the length in correct? 如果您使用传递的长度进行迭代,那会更好,这就是为什么您正确传递长度的原因? So you could use this: 因此,您可以使用以下代码:

void map(intModifier func, int src[], int dst[], int length){

    for (int i = 0; i < length; i++) // Iterate until we have met the length of the source
    {                                // array. Pass the source array off to our triple func
        dst[i] = func(src[i]);
    }

    return;
}

The function iterates through source and passes its contents to the triple func then stores it at the same location in our destination array. 该函数遍历源代码并将其内容传递给triple函数,然后将其存储在目标数组中的相同位置。

EDIT: Credit given to 1201ProgramAlarm and Ben . 编辑: 归功于1201ProgramAlarmBen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM