0

I'm having trouble understanding why my array of vectors is not inputting a line. ...

#include<bits/stdc++.h>
using namespace std;
int main () {
    int r;
    cin>>r;
    vector <int> v[r];
    for (int i=0; i<r; i++) {
        for (int j=0; j<i; j++) {
        int x;
        cin>>x;
        v[i].push_back(x);
        }
    }
    for (int i=0; i<r; i++) {
        for (size_t j=0; j<v[i].size(); j++){
            cout<<v[i][j];
        }    
        cout<<endl;
    }

}

... With input ...

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

... it outputs ...

7
38
810
2744

... with an empty line in the beginning of the output.

Bob Wang
  • 53
  • 4
  • Note that the use of variable length arrays (VLAs) is **not** standard C++ (although some implementations, like g++, allow them). Also see [Why should I not #include "bits/stdc++.h"](https://stackoverflow.com/q/31816095/10871073) – Adrian Mole Apr 05 '20 at 07:59
  • This construction `int r; cin>>r; vector v[r];` is a *variable length array* (VLA) and therefore not legal C++. VLAs are legal in C, but vectors are not, so it's not legal C either. Since you are using a vector, any reason not to use a vector throughout? `int r; cin>>r; vector> v(r);` would be perfectly good C++ code. – john Apr 05 '20 at 08:00

2 Answers2

2

You saw empty line at the beginning of output, because v[0] was empty. You can fix it this way:

#include<bits/stdc++.h>
using namespace std;
int main () {
    int r;
    cin>>r;
    // vector <int> v[r];
    //                ^
    // This is variable length array which is not really legal in C++. Use:
    vector<vector<int>> v;
    v.resize(r);
    for (int i=0; i<r; i++) {
        for (int j=0; j<=i; j++) {
        //             ^^
        // This loop had 0 iterations when i == 0, 1 when i == 1, ..., 4 when i == 4.
        // So you need to do one more iteration each time.
            int x;
            cin>>x;
            v[i].push_back(x);
        }
    }
    for (int i=0; i<r; i++) {
        for (size_t j=0; j<v[i].size(); j++){
            cout<<v[i][j];
        }    
        cout<<endl;
    }

}

Also:

#include <iostream>
#include <vector>
fas
  • 1,313
  • 8
  • 16
0

The issue is just in one line (commented on the body of code below). Also I changed your code to be more c++ alike.

//#include<bits/stdc++.h> //Include all libraries is a bad practice. Include just what you need
#include <vector>
#include <iostream>

using namespace std;

int main () {
    int r;
    cin >> r;

    vector<vector<int>> v(r, vector<int>());    //more secure than c-style vector<int> v[r]
                                                //and create the 1st level of vector
    for (int i = 0; i < r; ++i)
         for (int j = 0; j < i; ++j) {          //Here is the issue
             v[i].emplace_back();
             cin >> v[i].back();
         }

    for (auto& i : v) {                         //Iterate over the 1st level of vector
        for (auto& j : i)                       //Iterate over the 2nd level of vector
            cout << j << ' ';
        cout << '\n';
    }

    return 0;
}
TheArchitect
  • 975
  • 2
  • 14
  • 23