Initializing Error: How to create a multi dimensional zero array in C++ -
this code written in c++ make multi dimensional array gives initializing error. size of array should given input console user , not constant value. problem , solution? lot.
#include <iostream> using namespace std; int main() { int , b ; cout << "a: " << endl; cin >> ; cout << "b: " << endl; cin >> b ; int data[a][b] = {{0}}; return 0; }
the size of array should given input console user , not constant value.
this not possible in c++. suitable replacement using arrays using std::vector
. can use:
int = 10, b = 4; std::vector<std::vector<int>> data(a, std::vector<int>(b, 0));
if using pre-c++11 compiler, you'll need have space between 2 >>
.
std::vector<std::vector<int> > data(a, std::vector<int>(b, 0));
Comments
Post a Comment