2008-04-13

C++ Dynamic Array Test

Note:

The elements of a dynamically allocated array can be initialized only to the default value of the element type. The elements cannot be initialized to separate values as can be done for elements of an array variable.

To initialize elements,

method 1:

std::cin >> n; std::vector<int> **vec = new std::vector<int> *[n]; for (int i=0;i<n;i++) //initialize each vector vec[i]=new std::vector<int>(i); // use first vector vec[0]->push_back(something);

method 2:

std::cin >> n; // vectors are initialized by default constructor std::vector<int> *vec = new std::vector<int>[n]; for (int i=0;i<n;i++) // change size of each vector (vec+i)->resize(i); // equivalent to vec[i].resize(i) // use first vector vec[0].push_back(something);

No comments:

Post a Comment