I have an array like that:
int sizes[5] = {1,2,3,4,5};
I want to have 5 different arrays by using the array above. Because I want each of my arrays to have the size of sizes[i] (0 <= i < 5). How can I do that? The more I ponder upon this, the more I get confused. Do I have to use multidimensional arrays or just a simpler solution? Thanks in advance.
Simple, you just need dynamic memory allocation:
This is a simple thing to start with, but in real life the STL libraries are much more useful. In this code the memory is dynamically allocated, but not released. This can result into a memory leak. Using
std::vectorfrom STL library will save you from the hassle, and provide a easier way.@Mikhail has shown a good demonstration of using
vectors below.