Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting elements in multidimensional Vector

Tags:

c++

vector

vector<vector<int>> sort_a;
vector<int> v2;
vector<int> v3;

for (int i=0; i<4; ++i) {
v2.push_back(i);

  for (int j=0; j<4; ++j) {
  v3.push_back(j);
  sort_a.push_back(v2);
  sort_a.push_back(v3);
  }

}

Vector sort_a should be a 4x4 array, instead the output is 31x1 with lots of empty elements, how do i insert elements in a multidimensional vector ?

like image 226
Gambit King Avatar asked Dec 18 '12 15:12

Gambit King


People also ask

How do you add an element to a 2D vector?

Elements can be inserted into a vector using the push_back() function of C++ STL. Below example demonstrates the insertion operation in a vector of vectors. The code creates a 2D vector by using the push_back() function and then displays the matrix.

How do you add elements to a vector set?

Get the vector to be converted. Create an empty set, to store the result. Iterate through the vector one by one, and insert each element into the set. Print the resultant set.

How do you add all vector elements together?

Description. S = sum( A ) returns the sum of the elements of A along the first array dimension whose size does not equal 1. If A is a vector, then sum(A) returns the sum of the elements. If A is a matrix, then sum(A) returns a row vector containing the sum of each column.

Can you add elements to front vector?

Keep in mind that pushing stuff to the front of the vector is an O(n) operation, so if you need to do it repeatedly you probably want to use a data structure better optimized for that (such as std::deque ), or use other tricks (e.g. if you only add and remove stuff at the front, just do that at the end and display it ...


2 Answers

Don't think of it as a multidimentional vector, think of it as a vector of vectors.

int n = 4;
std::vector<std::vector<int>> vec(n, std::vector<int>(n));

// looping through outer vector vec
for (int i = 0; i < n; i++) {
  // looping through inner vector vec[i]
  for (int j = 0; j < n; j++) {
    (vec[i])[j] = i*n + j;
  }
}

I included parentheses in (vec[i])[j] just for understanding.

Edit:

If you want to fill your vector via push_back, you can create a temporary vector in the inner loop, fill it, and then push_back it to your vector:

for (int i = 0; i < n; i++) {
  std::vector<int> temp_vec;

  for (int j = 0; j < n; j++) {
    temp_vec.push_back(j);
  }

  vec.push_back(temp_vec);
}

However, push_back calls result in slower code, since not only you need to reallocate your vector all the time, but also you have to create a temporary and copy it.

like image 164
prazuber Avatar answered Sep 21 '22 02:09

prazuber


a vector<vector<int>> is not the best implementation for a multidimensional storage. The following implantation works for me.

template<typename T>
class array_2d {
    std::size_t data;
    std::size_t col_max;
    std::size_t row_max;
    std::vector<T> a;
public:
    array_2d(std::size_t col, std::size_t row) 
         : data(col*row), col_max(col), row_max(row), a(data)
    {}

    T& operator()(std::size_t col, std::size_t row) {
        assert(col_max > col && row_max > row)
        return a[col_max*col + row];
    }
};

use case:

array_2d<int> a(2,2);
a(0,0) = 1;
cout << a(0,0) << endl;

This solution is similar to the one described here.

like image 33
andre Avatar answered Sep 18 '22 02:09

andre