Message from C, C++ talks

July 2019

— You can use std::vector if you're using C++ and call resize on it.

Or you can dynamically allocate the array if you're using C. Don't forget to delete it manually at the end.

Message permanent page

— I saw this on stackoverflow but i dont have any idea of this concept

IS there any other way

— Which one? Do you use C or C++?

— C++


Well, std::vector is a template-based wrapper on dynamically allocated arrays, that you can resize dynamically, and use lots of things like alghorithms from STL. Also it has nice optimizations that go out of the box.

To use it you basically create an std::vector object giving it your type. Like std::vector<int> is something like a dynamic array of int elements.

Then you can work with it as with any array via square brackets. Or use other things like iterators. You can get the current size of array calling size function on it.

What else is not clear?

— Other way would be to have a very big array and use only part of it (store the size that you're actually using in another variable).

Message permanent page

— But that comes with wasting memory and size limitations.

— How would i access it?

array[vector<int>] ??

— This is kind of what i wanted

— You could open the second link I gave you. It contains working examples

— YEs I know but I am solving a small problem for hackerrank

— Okay
I will see

— Thanks!

— Ops, I meant the first link

— 👍