1

I have a function that needs data in a std::vector but I have them as different data. so I have this code:

void UseData(int x, int y, int z)
{
     std::vector<int> data;
     data.pushback(x);
     data.pushback(y);
     data.pushback(z);
     processData(data);
 }

Is there any better way to put the data inside the std::vector?

mans
  • 17,104
  • 45
  • 172
  • 321
  • 1
    IMO it would be a little better to reserve space in your vector before pushing any data. That way you won't have to reallocate memory each time your vector gets an element added. – Logicrat Sep 21 '15 at 16:10

1 Answers1

3

With C++11, you can use an std::initializer_list:

void UseData(int x, int y, int z) {
    std::vector<int> data {x,y,z};
    process(data);
}
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182