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

  • Context: C/C++ 
  • Thread starter Thread starter Peter P.
  • Start date Start date
  • Tags Tags
    2d Vectors
Click For Summary
SUMMARY

This discussion focuses on creating and managing 2D vectors in C++. The user transitions from C to C++ and seeks guidance on utilizing vectors for multidimensional arrays. Key operations include resizing the vector with mymatrix.resize(numrows_new) to adjust the number of rows and ensuring new rows maintain the same size as existing ones with mymatrix.resize(numrows_new, vector(numcols)). Additionally, modifying the number of columns requires iterating through each row and resizing it accordingly.

PREREQUISITES
  • Understanding of C++ syntax and data structures
  • Familiarity with the C++ Standard Template Library (STL)
  • Knowledge of dynamic memory management in C++
  • Basic programming concepts related to arrays and loops
NEXT STEPS
  • Explore C++ STL vector documentation for advanced features
  • Learn about memory management and performance implications of using vectors
  • Investigate alternatives to vectors, such as arrays and lists, for specific use cases
  • Practice implementing 3D vectors and other multidimensional data structures in C++
USEFUL FOR

C++ developers, software engineers transitioning from C to C++, and anyone looking to efficiently manage multidimensional arrays using vectors.

Peter P.
Messages
23
Reaction score
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
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:

Similar threads

  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 29 ·
Replies
29
Views
8K
Replies
1
Views
2K