0

I'm creating a 2d vector array of char as a class variable, but I'm having troubles of adding a vector into the vector< vector > array.

I'm running gcc with C++ 11 standard.

I tried using vector< vector<char> > row(size, vector<char> ); but errors will show saying I did not define size and vector<char>. If I define it with int size and vector<char> col it will think it's a new function declaration since I'm putting it as a class variable.

So I use the usual method vector< vector<char> > row; and have a function to add another vector into it. So I use this->row[i].push_back( vector<char> col); but then an error saying error: expected primary-expression before ‘col’ appears. I also tried to use just this->row[i].push_back( vector<char> ); but the error is still there saying error: expected primary-expression before ‘)’. This might be dumb but I have no idea how to add a vector into a vector.

class Vector2d {
private:
  int size;
  vector< vector<char> > row;
public:
  void make2d();
};

void Vector2d::make2d() {
  for (int i = 0; i < this->size; i++) {
    this->row[i].push_back( vector<char> col );  // compile error here
    for (int j = 0; j < this->size; j++) {
      this->row[i][j];
    }
  }
  cout << "It works!" << endl;
}

I expect it to add the vector array into the vector array, and that I can use row[i][j] for the rest of the program. But it gives a compiler error saying error: expected primary-expression before ‘col’ I have no idea what to do.

Anthony Kung
  • 1,025
  • 3
  • 12
  • 22
  • Unrelated: You may get better performance from a single `vector` of size `size*size` and a function that performs a 2D<->1D translation (`row * size + column`) because of better [spatial locality](https://en.wikipedia.org/wiki/Locality_of_reference) and fewer pointers to chase. [See this answer for details](https://stackoverflow.com/a/2076668/4581301). – user4581301 May 27 '19 at 02:54
  • Plus the code is often much simpler. – user4581301 May 27 '19 at 02:55

1 Answers1

5

vector<char> col is not a valid expression. It almost looks like a declaration of a variable (which is not what you need here), except it lacks a semicolon to complete the statement.

Furthermore, you cannot access row[i] until row actually contains at least i elements. row[i].push_back attempts to push a char element into a std::vector<char> that is the ith element of the vector of vectors (except you never created the ith element).

Given that in the following loop you appear to assume that the inserted vector should have size elements, here is how to insert a single vector of size elements into a vector of vectors:

row.push_back(std::vector<char>(size));
// or more simply
row.emplace_back(size);

If you would like to insert an empty vector instead, simply use:

row.emplace_back();

And here is how you can insert all size vectors of size elements in one go so that you can use row[i][j] for all i and j that are less than size:

row.resize(size, std::vector<char>(size));
user4581301
  • 33,082
  • 7
  • 33
  • 54
eerorika
  • 232,697
  • 12
  • 197
  • 326