Creating 2D Vectors in C++: A Quick Guide

  • C/C++
  • Thread starter Peter P.
  • Start date
  • Tags
    2d Vectors
In summary, the conversation discusses the switch from C to C++ and the use of vectors for multidimensional arrays. The individual asks for assistance with creating, resizing, and deleting a 2D vector and is directed to a thread with an example. The process of changing the size of a vector is also explained.
  • #1
Peter P.
23
0
I decided to make the switch from C to C++ and I'm having trouble with vectors. In C, I prefer to use dynamically allocated arrays when doing multidimensional arrays. But in C++, I know that there is the new function which replaced malloc. I read that when dealing with multidimensional arrays in C++, ideally a vector would be used.

So my question is, can someone just provide a quick run through of creating a 2D vector, changing the size of the vector, and then deleting it? Thank you in advance, any help is much appreciated.
 
Technology news on Phys.org
  • #2
See the following thread for an example:

https://www.physicsforums.com/showthread.php?t=509358

One way to change the number of rows in the example linked above:
Code:
mymatrix.resize (numrows_new);
If this adds new rows (i.e. you increase the size), they'll be empty (zero length). If you want the new rows to have the same size as the existing ones:
Code:
mymatrix.resize (numrows_new, vector<double>(numcols));
In either case, if you decrease the number of rows, they're "chopped off" the end (bottom) of the matrix.
To change the number of columns, you have to change the size of each row:

Code:
for (int row=0; row<numrows; row++)
{
    mymatrix[row].resize(numcols_new);
}
 
Last edited:

1. What is a 2D vector in C++?

A 2D vector in C++ is a data structure that stores a sequence of elements in two dimensions. It is similar to a 1D vector, but instead of being organized in a single line, it is organized in a grid-like structure with rows and columns.

2. How do I create a 2D vector in C++?

To create a 2D vector in C++, you first need to declare a vector variable using the vector keyword, specifying the data type of the elements it will hold. Then, you can use the push_back function to add elements to the vector. To create a 2D vector, you will need to use nested for loops to add elements to each row and column.

3. How do I access elements in a 2D vector?

To access elements in a 2D vector, you can use the at or [] operator, specifying the row and column indices of the element you want to access. For example, to access the element at the second row and third column, you can use myVector.at(1).at(2) or myVector[1][2].

4. Can I resize a 2D vector in C++?

Yes, you can resize a 2D vector in C++ using the resize function. This function takes in the desired number of rows and columns as arguments and resizes the vector accordingly. Keep in mind that this function will also initialize any new elements to their default values.

5. How do I delete a 2D vector in C++?

To delete a 2D vector in C++, you can simply call the clear function on the vector variable. This will remove all elements from the vector and free up the memory allocated for it. Alternatively, you can also use the pop_back function to remove elements from the vector one by one.

Similar threads

  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Linear and Abstract Algebra
Replies
9
Views
577
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top