How to implement a 2D vector array?

I am using the vector class in the STL library for the first time. How to add a vector array to a specific line?

struct x{ vector <vector <int> > v; int row; }; vector< int* > my ints; int add; 

If I wanted to add the first integer pointer to the first line v, can I do

 myints[0]->v[myints[0]->row].push_back(add); 

Is this method great for creating a 2 D vector of an int vector, where each row can potentially have a different length (i.e. have a different number of columns)?

+8
c ++ vector
source share
8 answers

I’m not quite sure what the problem is, since your sample code has several errors and doesn’t really make it clear what you are trying to do. But here is how you add a 2D vector to a specific line:

 // declare 2D vector vector< vector<int> > myVector; // make new row (arbitrary example) vector<int> myRow(1,5); myVector.push_back(myRow); // add element to row myVector[0].push_back(1); 

Does this answer your question? If not, can you try to clarify what you are facing?

+45
source share

If you know in advance the (maximum) number of rows and columns, you can use resize() to initialize the vector of vectors, and then change (and access) the elements using operator[] . Example:

 int no_of_cols = 5; int no_of_rows = 10; int initial_value = 0; std::vector<std::vector<int>> matrix; matrix.resize(no_of_rows, std::vector<int>(no_of_cols, initial_value)); // Read from matrix. int value = matrix[1][2]; // Save to matrix. matrix[3][1] = 5; 

Another possibility is to use only one vector and divide the identifier into several variables, get access, for example, vector[(row * columns) + column] .

+22
source share

Just use the following methods to create a two-dimensional vector.

 int rows, columns; // . . . vector < vector < int > > Matrix(rows, vector< int >(columns,0)); 

OR

 vector < vector < int > > Matrix; Matrix.assign(rows, vector < int >(columns, 0)); //Do your stuff here... 

This will create a matrix the size of rows * columns and initialize it with zeros , because we pass zero (0) as the second argument in the constructor, i.e. vector <int> (columns, 0) .

+4
source share

// initialize the 2D vector first

vector<vector<int>> matrix;

// initialize the 1D vector you want to insert into the matrix

vector<int> row;

// initialize the string with values

row.push_back(val1);

row.push_back(val2);

// now insert the values ​​into the matrix

matrix.push_back(row);

// output- [[Value1, Value2]]

+4
source share

Another way to define a two-dimensional vector is to declare a pair vector.

  vector < pair<int,int> > v; **To insert values** cin >> x >>y; v.push_back(make_pair(x,y)); **Retrieve Values** i=0 to size(v) x=v[i].first; y=v[i].second; 

For 3D vectors, take a look at the tuple and make_tuple.

+2
source share

We can easily use the vector as a 2d array. For this purpose we use the resize () method. The code below may be helpful in understanding this issue.

Code snippet:

 #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int row, col; cin>>row>>col; vector <vector<int>> v; v.resize(col,vector<int>(row)); //v = {{1,2,3}, {4,5,6}, {7,8,9}}; /** input from use **/ for(int i=0; i<row; i++) { for(int j=0; j<col; j++) { cin>>v[i][j]; } } for(int i=0;i<row; i++) { for(int j=0;j<col;j++) { cout<<v[i][j]<<" "; } } return 0; } 
+2
source share

I am using this piece of code. works great for me. copy it and run it on your computer. you yourself will understand.

 #include <iostream> #include <vector> using namespace std; int main() { vector <vector <int> > matrix; size_t row=3 , col=3 ; for(int i=0,cnt=1 ; i<row ; i++) { for(int j=0 ; j<col ; j++) { vector <int> colVector ; matrix.push_back(colVector) ; matrix.at(i).push_back(cnt++) ; } } matrix.at(1).at(1) = 0; //matrix.at(columns).at(rows) = intValue //printing all elements for(int i=0,cnt=1 ; i<row ; i++) { for(int j=0 ; j<col ; j++) { cout<<matrix[i][j] <<" " ; } cout<<endl ; } } 
+1
source share

If you are more interested in an elegant solution, you can try VectorEx from here: https://github.com/gileli121/VectorEx

In VectorEx.h, you have already created a 2D array data structure that you can easily use (e.g. std :: vector). This solution was implemented at a low level: std::vector<std::vector<DataType>>

From your point of view, you see this as vectorex::vector2d<DataType> aArray

Then to build a 2d array, for example, with 100 rows and 100 columns, you do aArray.build(100, 100); . He will build an array.

The second important advantage is a function that can display a 2d array as you open microsoft excel and display the array in it.

Function vectordisplay::DisplayVector_2d in VectorDisplay.h

This function will work under windows. tested on Windows 7 64 bit.

Please note that this project is new and errors are possible. function names may change over time.

Here are examples of how to use it:

 // includes.functions #include <windows.h> // Include CU3 Library // Get the files from https://github.com/gileli121/CU3-Library/ #include "CU3_Library\EasyCoding\easy_macros.h" // Include VectorEx https://github.com/gileli121/VectorEx #include "VectorEx\VectorEx.h" #include "VectorEx\VectorDisplay.h" using namespace vectorex; using namespace vectordisplay; #include "WndProc.h" /* This example shows how to use vector vectorex::vector2d array datatype, and how to display it using vectordisplay::DisplayVector_2d */ int main() { // Create 2D vector of strings vector2d<std::string> aArray; // Initialize the 2D vector. *You must do this before using the vector2d! aArray.build(10, 5); // Get rows count and columns count and print it pp "The number of rows is " << aArray.iSize_rows npp "And the number of columns is " << aArray.iSize_cols << std::endl ee; // Display the vector2d. *You must to give the same datatype in the "<>" DisplayVector_2d<std::string>(&aArray,"Display the vector 2D before changes"); // Assign some values // Method 1: aArray.assign(0, 0, "STR1"); // Assign to [row 0,col 0] the value "STR1". aArray.assign(0, 1, "STR2"); // Assign to [row 1,col 1] the value "STR2". aArray.assign(0, 2, "STR3"); // Assign to [row 2,col 2] the value "STR3". aArray.assign(0, 3, "STR4"); // Assign to [row 3,col 3] the value "STR4". aArray.assign(0, 4, "STR5"); // Assign to [row 4,col 4] the value "STR5". // Method 2 - From pointer: std::string stringStr = "string from pointer"; int iRow, iColumn; for (iRow = 1; iRow < aArray.iSize_rows; iRow++) { for (iColumn = 0; iColumn < aArray.iSize_cols; iColumn++) { aArray.assign(iRow, iColumn, &stringStr); } } // Method 1 (again): aArray.assign(5, 3, "STR6"); // Assign to [row 5,col 3] the value "STR6". aArray.assign(6, 2, "STR7"); // Assign to [row 6,col 2] the value "STR7". aArray.assign(7, 1, "STR8"); // Assign to [row 7,col 1] the value "STR8". aArray.assign(8, 0, "STR9"); // Assign to [row 8,col 0] the value "STR9". // Print Methods // Method 1: pp "The value in row 0, col 1 is " << aArray.at(0, 1) npp "And the value in row 1, col 1 is " << aArray.at(1, 1) << std::endl ee; // Method 2 (to pointer): std::string stringTmp; aArray.at(0, 4, &stringTmp); pp "The value in row 0, col 4 is " << stringTmp ee; aArray.at(7, 1, &stringTmp); pp "And the value in row 7, col 1 is " << stringTmp ee; // Display the vector2d again to see the changes DisplayVector_2d<std::string>(&aArray); return 0; } 

Example 1 vector display

Example 2:

 // includes.functions #include <windows.h> // Include CU3 Library // Get the files from https://github.com/gileli121/CU3-Library/ #include "CU3_Library\EasyCoding\easy_macros.h" // Include VectorEx https://github.com/gileli121/VectorEx #include "VectorEx\VectorEx.h" #include "VectorEx\VectorDisplay.h" using namespace vectorex; using namespace vectordisplay; /* This example shows how to use display std::vector array datatype with vectordisplay::DisplayVector_2d */ int main() { vector2d<std::string> aTest; std::stringstream ssTmp; aTest.build(100, 100); for (int r = 0; r < aTest.iSize_rows; r++) { for (int c = 0; c < aTest.iSize_cols; c++) { ssTmp.str(""); ssTmp << "STRING " << r + c; aTest.assign(r, c, &ssTmp.str()); } } DisplayVector_2d(&aTest); return 0; } 

Result of DisplayVector_2d

Example 3:

 int main() { vector2d<std::string> aTest; // Declare the 2D array aTest.build(10, 5); // build the 2D array DisplayVector_2d(&aTest); // Show aTest.resize(100, 100); // Resize DisplayVector_2d(&aTest); // Show aTest.overwrite("AAA"); // Write "AAA" on all the array DisplayVector_2d(&aTest); // Show aTest.resize(50, 50); // Resize DisplayVector_2d(&aTest); // Show aTest.resize(100, 100); // Resize DisplayVector_2d(&aTest); // Show aTest.unbuild(); /* Free memory *Don't use the array after unbuild. to use again, you need to build() it again!*/ vector2d<int> aTest2; // Declare the 2D array of ints aTest2.build(100, 100); DisplayVector_2d(&aTest2); // Show aTest2.overwrite(100, 10, 5, 60, 30); /* Write the value 100 on rows 10-60, and on cols 5-30 */ DisplayVector_2d(&aTest2); // Show return 0; } 

Hope you found this helpful.

For more information and an example, go to https://github.com/gileli121/VectorEx and <a4>

NOTE: initialize() changed to build() . Ignore "initialize" on images

0
source share

All Articles