-1

Let's say i have a lot of vectors :


vector<int> v1 = ...
vector<int> v2 = ...
...
vector<int> v1000 = ...

Is there a quicker way to populate a 2D vector with those 1D vectors than this :

vector<vector<int>> vGlobal ;
vGlobal.push_back(v1);
vGlobal.push_back(v2);
.......
VGlobal.push_back(v1000);

genpfault
  • 51,148
  • 11
  • 85
  • 139
Viroun
  • 1
  • 1
    Yes, it's called a 'loop', a programming construct that lets you repeat a block of code multiple times. – Rinat Veliakhmedov Oct 14 '20 at 08:55
  • @RinatVeliakhmedov The loop is not quicker. – Vlad from Moscow Oct 14 '20 at 08:59
  • 2
    You could do `std::vector> vGlobal{v1, v2, v3};` but _why_ do you have a lot of separate vectors to start with? Why not _only_ have `vGlobal` and index in that? `vGlobal[0]` etc...? – Ted Lyngmo Oct 14 '20 at 09:00
  • @VladfromMoscow it is surely quicker than writing a 1000 pushbacks. – Rinat Veliakhmedov Oct 14 '20 at 15:08
  • @RinatVeliakhmedov "Yes it's called a loop". Don't know if it's sarcastic but i guess it is...so what would you iterate on ? I'm curious about your reply, please show me your skills – Viroun Oct 14 '20 at 16:59
  • The dupe is about `int`, but exactly the same thing works for `vector`. – cigien Oct 14 '20 at 18:05
  • @Viroun I noticed your deleted answer to this question: "_But this means you have to first populate your vector v with v1,v2,...v1000. Which means pushing back 1000 Times_" - No, what I wrote in my answer creates `1000` vectors so you don't need to `push_back` anything. `v[0]` ... `v[999]` are ready to use directly after `std::vector> v(1000);` – Ted Lyngmo Oct 21 '20 at 23:04

2 Answers2

1

It will be faster to

  • first reserve 1000 slots and
  • then move the vectors into vGlobal (iff v1 to v1000 aren't needed anymore; if you still need v1 to be "intact" after the procedure do not use std::move)

if you cannot alter the structure.

vector<vector<int>> vGlobal;
vGlobal.reserve(1000);
vGlobal.push_back(std::move(v1));
vGlobal.push_back(std::move(v2));
...
VGlobal.push_back(std::move(v1000));

If you can alter the structure then I'd advise to use std::vector<std::vector<int>> to hold all 1000 instances in the first place and not go for v1, v2, ..., v1000.

Pixelchemist
  • 24,090
  • 7
  • 47
  • 71
0

Let's say i have a lot of vectors :

vector<int> v1 = ...
vector<int> v2 = ...
...
vector<int> v1000 = ...

Let's pretend that you don't have to manage and name them individually and instead of v1 to v1000 you could write v[0] to v[999] which makes it easy to process all of them in a loop etc?

There are standard solutions for this:

  1. If you have a fixed number of these vectors:
    std::array<std::vector<int>, 1000> v;
    
  2. If you may want to add/remove vectors in run-time:
    std::vector<std::vector<int>> v(1000);
    

Now, if you for some reason would like to create vGlobal with a copy of all the vectors in v:

std::vector<std::vector<int>> vGlobal(v.begin(), v.end());
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108