2

I am learning recursion in c++ and was stuck on why you cant simply use the .push_back() instead of creating a function to copy the specific_previous_result elements, then .push_back().

vector<vector<int>> get_every_n_elements(vector<int> arr, int n) {
if (n == 0) {
    vector<vector<int>> result;

    vector<int> empty_list;
    result.push_back(empty_list);

    return result;
}

vector<vector<int>> previous_result = get_every_n_elements(arr, n - 1);

vector<vector<int>> current_result; //empty

for (auto specific_previous_result : previous_result) { // [[]] -> []

    for (auto elem : arr) { // [1,2,3,4] -> 1
      //current_result.push_back(specific_previous_result.push_back(elem));
      //This does not work^^

      current_result.push_back(group(specific_previous_result, elem));
      //The group function copies all elements to newVec and push_back(elem) after
      //Then returns newVec with elem at the end
    }

}

return current_result;
}

The error that I get when I run the push_back line is error: invalid use of void expression current_result.push_back(specific_previous_result.push_back(elem));. Thank you for your help.

  • even if it did work it would do what you expect, because `specific_previous_result` is a copy of the vector, you want `for (auto& specific_previous_result : previous_result)` instead – 463035818_is_not_an_ai Oct 10 '17 at 18:03
  • and what is that line supposed to mean anyhow? You are trying to push the return value of another `push_back`, but `push_back` returns `void` – 463035818_is_not_an_ai Oct 10 '17 at 18:04

2 Answers2

5

There doesn't seem to be a valid reason to return the vector itself after a push_back. Sometimes it's useful, but most of the time, it is not. I would recommend writing it in two lines, which is also more clearer IMO than a separate (and inefficient!) function:

current_result.push_back(specific_previous_result);
current_result.back().push_back(elem);
Rakete1111
  • 47,013
  • 16
  • 123
  • 162
3

The reason why that line is causing the compiler to give you the invalid use of void expression current_result.push_back(specific_previous_result.push_back(elem)); error is trivial. Take a look at the line in question:

current_result.push_back(specific_previous_result.push_back(elem));

The bolded part is calling the vector's push_back function, a function which has a void return type. You are attempting to pass this void return value as a parameter to current_result.push_back.

See the documentation for std::vector::push_back. As you can see, the return value for both overloads is void.

You said it yourself, your group function is returning a vector which you are then pushing onto the back of your current_result vector. This is why the line which uses the group function compiles.

bfair
  • 1,101
  • 7
  • 16
  • O I see, I did not know that `std::vector::push_back` returned void. I am coming from Python and was under the impression it returned a copy of the vector with the new element appended to the end. – javiercaudillo10 Oct 10 '17 at 19:15