How to implement c++ 2d vector
/* C++ code to demonstrate a 2D vector
with elements(vectors) inside it. */
#include <iostream>
#include <vector>
using namespace std;
int main()
{
/*
Below we initialize a 2D vector
named "vect" on line 12 and then
we declare the values on
line 14, 15 and 16 respectively.
*/
vector<vector<int>> vect
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
/*
Now we print the values that
we just declared on lines
14, 15 and 16 using a simple
nested for loop.
*/
for (int i = 0; i < vect.size(); i++)
{
for (int j = 0; j < vect[i].size(); j++)
{
cout << vect[i][j] << " ";
}
cout << endl;
}
return 0;
}
c++ 2d vector initialization list
vector<vector<int>> myvect = { {10,20,30,40},
{50,60,70,80} };
vector<vector<int>> myvect{ vector<int>{10,20,30,40},
vector<int>{50,60,70,80} };
c++ vector
std::vector<dataType> v;
std::cout << v.max_size();
A vector is a sequence container class that implements dynamic array, means size automatically changes when appending elements.
Erase: All the elements of the vector are removed using clear() function. erase() function
Max size: max_size() is the theoretical maximum number of items that could be put in your vector. On a 32-bit system, you could in theory allocate 4Gb == 2^32 which is 2^32 char values, 2^30 int values or 2^29 double values.
c++ 2d vector class
postnet(vector<int> const &zip)
: pncode(5, vector<int>(5))
{
encode(zip);
}
postnet(vector<vector<int> > const &pnc = vector<vector<int> >(5, vector<int>(5)))
{
assert(pnc.size()==5);
for (int i=0;i<pnc.size();++i)
assert(pnc[i].size()==5);
pncode = pnc;
}
c++ 2d array of vectors
int** ary = new int*[rowCount];
for(int i = 0; i < rowCount; ++i)
ary[i] = new int[colCount];
c++ 2d vector push_back
vector <vector <BigInt> > matr;
for (BigInt i=0;i<rij;i++) {
for (BigInt j=0;j<kolom-1;j++) {
matr.push_back().push_back((i+1)^(pow-j));
}
}
dynamic 2d vector c++
#include <vector>
#include <iostream>
auto make_one(int rows, int cols) {
return std::vector<std::vector<int>>(rows, std::vector<int>(cols));
}
int main() {
auto v = make_one(5, 3);
v[2][1] = 7; // demo storing a value
for(auto& r : v) { // demo output
for(int n: r) std::cout << n << ' ';
std::cout << '\n';
}
}
3d vector c++
#include<vector>
#include<iostream>
#include<ctime>
using namespace std;
#define DIM1 100
#define DIM2 9
#define DIM3 120
int main()
{
clock_t t1_start = clock();
vector<vector<vector<string>>> vec1(DIM1, vector<vector<string>>(DIM2, vector<string>(DIM3)));
clock_t t1_end = clock();
double diff1 = (t1_end - t1_start) / double(CLOCKS_PER_SEC);
clock_t t2_start = clock();
vector<vector<vector<string>>> vec2;
vec2.resize(DIM1);
for(int i = 0; i < DIM1; i++)
{
vec2[i].resize(DIM2);
for(int j = 0; j < DIM2; j++)
vec2[i][j].resize(DIM3);
}
clock_t t2_end = clock();
double diff2 = (t2_end - t2_start) / double(CLOCKS_PER_SEC);
cout<<"1st definition used time: "<<diff1<<"s"<<endl;
cout<<"2nd definition used time: "<<diff2<<"s"<<endl;
}
how to take input in 2d vector in c++
for(int i=0; i<n; i++){
jail[i].reserve(n);
for(int j=0; j<n; j++){
cin>>jail[i][j];
}
}