Home c++ 2D vector

2D vector

Author

Date

Category


Answer 1, authority 100%

If the sizes are known (or one):

vector & lt; vector & lt; T & gt; & gt; a (n, vector & lt; T & gt; (m, value)); // two sizes
vector & lt; vector & lt; T & gt; & gt; b (n, vector & lt; T & gt; ()); // one size

You can specify the value that you want to fill the vector with. If T is a class and no value is specified, then the default constructor will be used.

For example:

vector & lt; int & gt; c (n, -1); // vector filled with -1
vector & lt; my_class & gt; d (m, my_class ("Hello, World!", 42));

If the number of elements is not known in advance, then you can use push_back () :

vector & lt; vector & lt; T & gt; & gt; e;
e.push_back (vector & lt; T & gt; ());

Answer 2, authority 50%

# include & lt; iostream & gt; // This is how it should be
#include & lt; vector & gt; // For a vector
using namespace std; // In order not to suffer
int main ()
{
  int n, m; // Create variables responsible for the size of the vector
  cin & gt; & gt; n & gt; & gt; m; // Enter the dimensions of the vector
  vector & lt; vector & lt; int & gt; & gt; a (n, vector & lt; int & gt; (m)); // Declare a vector of n lines with m elements
  for (int i = 0; i & lt; n; i ++) // Loop that goes through the lines
    for (int j = 0; j & lt; m; j ++) // Loop that goes through the elements
    {
      cin & gt; & gt; a [i] [j]; // Populate the vector or array (in this case, input)
    }
  for (int i = 0; i & lt; n; i ++) // Loop that goes through the lines
  {
    for (int j = 0; j & lt; m; j ++) // Loop that goes through the elements
cout & lt; & lt; a [i] [j] & lt; & lt; ''; // Output the elements of the i row of the vector
    cout & lt; & lt; endl;
  }
  return 0;
}

Answer 3, authority 42%

vector & lt; vector & lt; int & gt; & gt; v;
v.push_back (vector & lt; int & gt; ());
v [v.size () - 1] .push_back (100500);
int a = v [i] [j];

Answer 4, authority 42%

vector & lt; vector & lt; int & gt; & gt; vvi;
// ... initial
for (vector & lt; vector & lt; int & gt; & gt; :: iterator it = vvi.begin (); it! = vvi.end (); ++ it) {
  for (vector & lt; int & gt; :: iterator it2 = (* it) .begin (); it2! = (* it) .end (); ++ it2) {
    // ... out
  }
}

Answer 5, authority 17%

If the array is rectangular and its dimensions are unchanged, then you can make a class that expands a one-dimensional array into a linear one:

template & lt; typename T & gt; class Array2D
{
public:
  Array2D (size_t w, size_t h): width (w), height (h), data (new T [w * h]) {}
  ~ Array2D () {delete [] data;}
  T * operator [] (size_t i) {return data + i * height;}
  const T * operator [] (size_t i) const {return data + i * height;}
private:
  T * data;
  size_t width, height;
}

Or using std :: vector inside:

template & lt; typename T & gt; class Array2D
{
public:
  Array2D (size_t w, size_t h): width (w), data (w * h) {}
  T * operator [] (size_t i) {return data.data () + i * height;}
  const T * operator [] (size_t i) const {return data.data () + i * height;}
private:
  std :: vector & lt; T & gt; data;
  size_t width;
  // we do not store the height, since if desired, it can be calculated by dividing data.size () by width
}

It behaves just like regular two-dimensional arrays, but its sizes are determined at run time, not at compile time, as with regular static arrays.
Line of code

Array2D & lt; int & gt; array (10, 20);

is similar to this static array declaration

int array [10] [20];

They are filled in the same way, for example, like this:

for (int i = 0; i & lt; 10; i ++)
  for (int j = 0; j & lt; 20; j ++)
    array [i] [j] = i + j;

The advantage of this class is that we allocate memory in one contiguous block, just like a static array in C. In addition, you can add array out-of-bounds checks to operator []. But you can’t check for out-of-line testing without complicating the class with proxies. This line out of bounds will give us the next line element.


Answer 6, authority 17%

Correct answer (array of n – rows, m – columns)

vector & lt; vector & lt; int & gt; & gt; matrix (n, vector & lt; int & gt; (m, 0));

Answer 7, authority 8%

Initialization (selected values ​​for example):

std :: vector & lt; std :: vector & lt; int & gt; & gt; A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

Output:

for (int i = 0; i & lt; A.size (); i ++)
{
  for (int j = 0; j & lt; A [i] .size (); j ++)
  {
    std :: cout & lt; & lt; A [i] [j];
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions