0

C++ Vectors

I failing to understand vectors l mean the whole topic

15th Aug 2025, 9:23 PM
Delroy Zemudzo
Delroy Zemudzo - avatar
5 Antwoorden
+ 6
Vector is essentially a dynamic array that can automatically resize itself and supports efficient element access, insertion, and deletion operations. Just assume it's a better version of array.
16th Aug 2025, 2:34 AM
SAN
SAN - avatar
+ 3
a short comparison from a ai: array has a fixed size, is stored on the stack, cannot be resized, and is very fast because it avoids heap allocations. It is best when the size is known at compile time. vector has a dynamic size, is stored on the heap, can be resized with operations like push_back, has a small overhead due to dynamic allocation (not as fast as areay), and is best when the size can change or is not known at compile time.
16th Aug 2025, 1:53 PM
nora
nora - avatar
+ 2
...[part2] comfortable as you wont hopefully need to resize ever hopefully. resizing is a costly part for using a vector. {inResponseTo: {message:"Accessing and insertion l understand it but deletion it's shaking me a lot"}}
16th Aug 2025, 11:16 PM
The Witcher
The Witcher - avatar
+ 1
Vectors are similar to Lists, just saying in case you would be more familiar with lists (especially if you come from a beginner python background). both are containers. both you can add stuff in. both you can remove stuff out. As you add stuff in a list, it requires one more chunk of space in dynamic memory. When you remove stuff from the list, the chunk of memory is deallocated at that same moment, that memory becomes available to the system again. in a list structure, all the memory chunks point to some other chunk(s). As for the vector, it works differently because a vector, there are no many chunks. the vector keep the different stuffes next to each other in a continuous single fat chunk of memory. as you add some stuff in your vector, the vector needs to use a bigger chunk of data when it is full. then it recopies all its data to that fatter and thiccer chunk of space. when you remove some stuff from a vector it will not resize that single fat chunk for a smaller chunk. fat chunk is more...
16th Aug 2025, 11:15 PM
The Witcher
The Witcher - avatar
0
Accessing and insertion l understand it but deletion it's shaking me a lot
16th Aug 2025, 7:09 AM
Delroy Zemudzo
Delroy Zemudzo - avatar