Is it possible to have a dynamic array size?
For example:
It wont compile but i was wondering if it's possible to achieve that some way.Code:...
int size;
cout<<"size: ";
cin>>size;
cin.ignore();
int array[size];
...
Thanks!
Printable View
Is it possible to have a dynamic array size?
For example:
It wont compile but i was wondering if it's possible to achieve that some way.Code:...
int size;
cout<<"size: ";
cin>>size;
cin.ignore();
int array[size];
...
Thanks!
A std::vector?
Oh... ha.
Never used a vector before.
I'll have to look into that i suppose ;)
Thanks!
If you want to have the user input a value that will serve to initialize the array only once, you can dynamically allocate memory using the new operator. If you want to change the array's size you want an std::vector or an std::list.
Code:int* array = new array[size]
// ...
// not to forget !
delete[] array;
array = 0;
Even if you don't need to change the array size, it is generally better to use a vector or other container in C++.