0

I am having some trouble/error appending to a vector in C++. My code is:

std::vector<int> maps(const std::vector<int> & values) {
  
  for(int i = 0; i < std::vector.size(); i++){
    values.pushback(maps[i] * 2);
  }
  return values;
}

My goal is to have a base list that holds some numbers, then create a new list containing those numbers doubled.

I'm getting an error, that is unfortunately too long to copy it in here. But I will give you the details:

Exit Code: 254
STDERR

Stack dump:
0.  Program arguments: /usr/lib/llvm-8/bin/clang -cc1 -triple x86_64-pc-linux-gnu -emit-obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-(this is where it gets a bit long)

The full code is(I am guessing, since this is a puzzle)

#include <vector>

std::vector<int> maps(const std::vector<int> & values){
  for(int i = 0; i < std::vector.size(); i++){
    values.push_back(maps[i] * 2);
  }
  return values;
}
int main(){
  return 0;
}
elipie
  • 17
  • 6
  • 1
    `for(int i = 0; i < std::vector.size(); i++){` -> `for(int i = 0; i < maps.size(); i++){` Confusing to have a function named `maps` as well as a variable somewhere. At least that's what I think you have. A [mcve] with more context would help narrow this down. In any case `std::vector.size()` is incorrect. – Retired Ninja Jul 13 '21 at 05:36
  • `pushback` does not exist. do you mean `push_back`? There is also no `maps` variable, only `values` – Raildex Jul 13 '21 at 05:37
  • 1
    I am so sorry for wasting your time, I am trying to learn vectors so it is a bit hard for me. Thank you for your Help @RetiredNinja – elipie Jul 13 '21 at 05:41
  • the error message for the code you posted isnt that long: https://godbolt.org/z/ePT4c8Pbx. Though I am not sure if this is the error message you are talking about. Please do include a [mcve] and the error message in the question – 463035818_is_not_an_ai Jul 13 '21 at 05:44
  • Also @Raildex I just realized that I am using the maps function (thinking it as a function name) as a variable/function name. – elipie Jul 13 '21 at 05:45
  • @463035818_is_not_a_number Huh, in my IDE it spit out a huge STDERR, I do not know the differences between the two, but something happened. – elipie Jul 13 '21 at 05:49
  • if the question has been solved with the help of comments you can delete it. If you want an answer you should post a [mcve]. – 463035818_is_not_an_ai Jul 13 '21 at 05:50
  • @463035818_is_not_a_number I do not get it. What is a minimal reproducible example, and how is it related to this question? I read the article and I still don't get how it is related – elipie Jul 13 '21 at 05:54
  • a minimal example is some code that others can copy and paste and compile to get the same error message as you do. You neither posted the complete code, nor the error message, so we can only guess what you are actually doing to get a long error message – 463035818_is_not_an_ai Jul 13 '21 at 05:55
  • Ah, I see. My bad. I get it now. Well the main function doesn't exist because this is a problem from a programming puzzle website. But I guess I could edit it and make it the same way. – elipie Jul 13 '21 at 05:58
  • 1
    If you want to create a new list you shouldn't push the values to the list you get as function argument, no? Not to mention you can't do that, because `values` is `const` (you can't modify it). I think you could use a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn the basics of C++. Programming puzzles are fun, but you can't learn C++ from them. – Lukas-T Jul 13 '21 at 06:07
  • thank you I will get a C++ book today. – elipie Jul 13 '21 at 14:29

2 Answers2

3

According to your question. This is what you want.

std::vector<int> maps_double(const std::vector<int> & values) {
   std::vector<int> doubles;
   for(int i = 0; i < values.size(); i++){
      doubles.push_back(values[i] * 2);
     }
   return doubles;
}

But in your code, you are doing several things wrong.

for(int i = 0; i < std::vector.size(); i++)

You are calling the size() on std::vector? No way. You need to have an object to call size on it. Probably you want to call like this:

for(int i = 0; i < values.size(); i++)

And then you are doing this:

values.pushback(maps[i] * 2);

you are declaring the maps function. And then you are using subscript to access the i index. which is wrong. And you are pushing the value to values but you want a new vector.

foragerDev
  • 1,307
  • 1
  • 9
  • 22
1

to add to what foragerDev did to simply correct your syntax,

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

really should use:

for (const v : values)
    doubles.push_back (v*2);

Don't use an old-fashioned counted loop when you really want to go through all the elements in a collection — there is a more direct way to do that.

But your function (as rewritten by foragerDev) is just a standard algorithm: transform (or ranges::transform if you have C++20).

JDługosz
  • 5,592
  • 3
  • 24
  • 45
  • Yes, I try to make it more understandable at the beginner level. But what you suggested is more readable, simple and take advantage of modern C++ features. – foragerDev Jul 13 '21 at 15:00