-1

In my head it seems like creating (a) vector by pushing n number of elements and then appending the (a) vector to the (b) vector to make a 2D vector. I'm still a rookie at C++ so can someone suggest me a better idea

#include <iostream>
#include<vector>
using namespace std;
int main() {
    int numOfIter;
    vector <int> a;
    vector <int> b;
    cout << "enter the number of iterations -> ";
    cin >> numOfIter;
    for(int i = 0; i < numOfIter; i++){
        a.push_back(i);
    }
    b.push_back(a);

    for(int it : b){
        cout << it;   
    }
    return 0;
}
James Z
  • 12,104
  • 10
  • 27
  • 43
XD.PY
  • 1
  • 1
    If you want `b.push_back(a);` to work, then `b` needs to be a vector of vector of `int`; not a vector of `int`. The remaining for-loop will likewise need attention. You may also find entering `[cpp] 2d vector` in any search box on this site may be informative, reeling in questions/answers [like this one](https://stackoverflow.com/questions/41400116/2d-vector-vs-1d-vector). – WhozCraig May 07 '22 at 09:22
  • `b` is a vector of `int`s and you are attempting to push a vector `a` to it. Not sure what you want, maybe you want to push all ints in a to b. So maybe you want `b.insert(std::end(b), std::begin(a), std::end(a));` – Blindman67 May 07 '22 at 09:24

0 Answers0