英文:
Sum of subsequences in cpp, I have to print all the sums of subsequences. I'm storing it in vector g here. result shows g is empty
问题
class Solution
{
public:
void f(vector<int> arr, int N, int i, int sum, vector<int> g){
if (i >= N){ // 当索引达到数组的末尾时
g.push_back(sum); // 将最终总和添加到向量g中
return;
} else {
sum = sum + arr[i]; // 将这个元素包括在总和中
f(arr, N, i + 1, sum, g);
sum = sum - arr[i]; // 不包括这个元素在总和中
f(arr, N, i + 1, sum, g);
}
}
vector<int> subsetSums(vector<int> arr, int N)
{
vector<int> g;
int sum = 0;
int i = 0;
f(arr, N, i, sum, g);
return g;
} // 主块将以排序顺序打印g
};
这是您提供的C++代码的翻译部分。以下是输入和预期输出的说明:
输入: {2, 3}, 2
预期输出: 0 2 3 5
英文:
>
> class Solution
> {
> public:
> void f(vector <int> arr, int N, int i , int sum, vector <int> g){
> if (i>= N){// when index reaches the end of the array
> g.push_back(sum);//add the final sum to vector g
> return;
> }else{
> sum= sum+arr[i]; //include this element in the sum
> f(arr,N, i+1,sum,g);
> sum = sum-arr[i];//do not include this element in the sum
> f(arr, N, i+1,sum,g);
> }
> }
> vector<int> subsetSums(vector<int> arr, int N)
> {
> vector <int> g;
> int sum=0;
> int i=0;
> f(arr, N,i,sum,g);
> return g;
> }//main block will print g in sorted order
> };
>
>
> THis is my code written in cpp to return the sum of all subsequences as an vector. this is a basic recursion problem . the vector g stores the final sums. But g found to be empty.
>
> input: {2,3} , 2
>
> expected : 0 2 3 5
答案1
得分: 2
正如Shaun提到的,你在函数f中将向量g按值传递。当你将总和插入向量g时,你是在一个局部对象上这样做的。因此,subsetSums()
中的向量g是空的。(尝试在调试自己的代码时打印有用的信息。)
你可以查阅有关按引用传递和按复制/值传递的信息:
https://www.ibm.com/docs/en/zos/2.4.0?topic=functions-function-calls
一个简单的更改将得到你期望的输出
void f(vector<int> &arr, int N, int i, int sum, vector<int>& g)
英文:
As Shaun mentioned, you are passing the vector g by value in your function f. When you insert the sum to vector g, you are doing so on a local object. Hence the vector g in subsetSums()
is empty. (Try printing useful information when debugging your own code.)
You can read up on pass-by-reference vs pass-by-copy/value:
https://www.ibm.com/docs/en/zos/2.4.0?topic=functions-function-calls
A simple change will have your desired output
void f(vector <int> arr, int N, int i , int sum, vector <int>& g)
答案2
得分: 0
C++中的参数默认为按值传递。因此,您传递了一个空向量g的副本,并在函数内部进行了本地填充。您可以通过在函数定义(而不是函数调用)中在g之前插入一个&符号来将g更改为按引用传递。
void f(vector<int> arr, int N, int i, int sum, vector<int>& g){
您可以在函数调用中看到&符号。您根本不需要更改函数调用。通过引用调用,您表示函数调用中传入的g与函数f中的g是“相同的”。在这种情况下,当您在函数中填充g时,它也会填充函数调用中的g。
在进行此编辑之前,而不是“相同的”g,它只是一个副本。在按值传递中,传入的参数被复制为值。因此,当函数编辑这些变量时,它们仅在函数本身内部可见。
最后,通常有利于始终将向量作为引用传递,以避免复制大量数据。在这种情况下,如果您知道函数不会修改向量,那么可以在向量前面放一个const。因此,如果f不会修改arr,您可以将其传递为“常量”引用。
void f(const vector<int>& arr, int N, int i, sum, vector<int>& g)
英文:
Arguments in C++ are call by value by default. So you're passing a copy of an empty vector g and filling it up locally in the function. You could alter g to be pass by reference by inserting an ampersand, &, before the g in the function definition (not the function call).
void f(vector <int> arr, int N, int i, int sum, vector<int>&g){
You can see the ampersand in the call there. You don't need to alter your function call at all. With call by reference, you're saying that the g passed in the function call is "the same" g as the one in the function f. In this case, when you fill that g in the function, it is also filling the one in the function call.
Before this edit though, instead of being "the same" g it was simply a copy. In call by value, the parameters passed in are copied as values. So when the function edits those variables, they are seen only locally by the function itself.
Lastly, it is often advantageous to always send vectors as call by reference to avoid copying large amounts of data. In this case, if you know the function will not change the vector, then put a const in front of the vector. So if f will not modify arr, you could make it a call by 'constant' reference.
void f(const vector<int> &arr, int N, int i, int sum, vector<int> &g){
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论