Understanding C++ Vector (Dynamic Arrays)

preview_player
Показать описание
In this video, we'll take a closer look at C++ vectors (STL implementation of a dynamic array).

We'll learn how C++ vectors are implemented, and code a very simple custom class with the absolute minimum needed from a dynamic array data structure.

std::vector is a sequence container that encapsulates dynamic size arrays in C++. The elements are stored contiguously, which means that elements can be accessed not only through iterators but also using offsets to regular pointers to elements.

The storage of the vector is handled automatically, being expanded as needed. Vectors usually occupy more space than static arrays, because more memory is allocated to handle future growth. This way a vector does not need to reallocate each time an element is inserted, but only when the additional memory is exhausted.

Chapters:
00:00:00 Introduction to C++ vector
00:01:40 C++ static arrays
00:08:11 Using the STL vector class
00:15:02 A short discussion on abstraction
00:16:47 Creating our own 'toy' Vector class
00:19:10 Vector methods and algorithm complexity
00:24:11 Vector using generics with C++ templates
00:28:28 Private members of our Vector class
00:32:33 Vector function prototypes and signatures
00:43:10 Constructors and destructors
00:48:13 Square-brackets operator overloading
00:48:59 Getter methods
00:49:54 Copy constructor
00:51:43 PushBack method
01:00:04 PopBack method
01:02:35 Clear method
01:03:06 Erase method
01:06:01 Assignment operator overloading
01:08:49 Insert method (proposed exercise)
01:10:05 Conclusion & next steps

For comprehensive courses on computer science, retro programming, and mathematics, visit:

Don't forget to subscribe to receive updates and news about new courses and tutorials:

Enjoy!
Рекомендации по теме
Комментарии
Автор

Thanks for this video! I'm currently taking your C++ 2D Game Engine course, and took this detour to better understand the Vector class, since I'm coming from a web development background where everything is much more abstracted. I made an insert implementation as follow:

void Insert(int index, T value){
//Handle the case where we're already at the max size before inserting

if(size == capacity){
T* newArray = new T[capacity * 2];
for (int i = 0; i < size; i++){
newArray[i] = elements[i];
}
delete[] elements;
elements = newArray;
capacity *= 2;
}

//Increase the size by 1
size++;

//Loop over the array in reverse, setting each value equal to the one before, stopping at the given index
for(int i = size - 1; i >= index; i--){
elements[i + 1] = elements[i];
}

//Replace the given index with the given value
elements[index] = value;
}

It's basic and there might be some edge cases I overlooked, but it seems to work from my testing.

abbasio
Автор

this is one of best c/cpp vectors tutorial

ayoubelmhamdi
Автор

Wow your knowledge about this is so deep. I'm trying to develop my skill and knowledge in programming as I'm a beginner in it. I've started C++ because some people said it's the hardest to learn so I thought why not? I like learning hard stuff but theres way too much in C++

achyuththouta
Автор

Eso es lo que le falta a tu canal de YouTube, algo desde lo básico y de ahí pasar a tus cursos

fabianrr
Автор

I really should've found your youtube channel earlier. I'm doing your assembly language course on udemy too. You are a very good teacher.

masabh
Автор

One more follower. I came through @Akitando channel. Very nice explanation.

phlimma
Автор

I recently started learning about vectors and I found this vid really helpful

Luard-xqzd
Автор

37:28 in lines 23 why it had a lot of const? first const and last const?

jackgame
Автор

i cant belive your channel just have 10k subscribers, your videos are so good!!

arturmg
Автор

I love how you kept the memories of the classic retro games alive.
Good stuff! :)
So, vector and C++ can work for console like NES? If so, that's really cool!

TheJGAdams
Автор

Great video. Do you know how to implement a vector class in C++ like that used in Tradestation's EasyLanguage which can accommodate multiple data types within the same vector?

graemereed
Автор

Every single one of your videos is awesome. Thank you!

gammyhorse
Автор

The title made me think this would be an in-depth look at the machinations of how the standard library's vector _really_ works, but this is a very basic introduction. Your element-removals do not even call any potential destructor :/

Dannnneh
Автор

Thanks a lot. It makes sense now. What do you suggest me if I decide to my own implement of stack and queue?

Miura-Anjin
Автор

Awsome! Do you have the dotfiles of your vi setup that you can share? Thanks

DanielTolentino
Автор

What about, if T doesn't have constructor? new T[2 * n] throws errror

dmitrysavkin
Автор

Dear Professor Pezzi
Thanks for the video! Very well explained!

If I wanted to do something like this in C. What do I have to pay attention to, besides checking whether there is enough memory (malloc). Are there things that I absolutely have to do? (You talked about the whole thing not being so trivial).

starcw
Автор

Hi Gustavo, do you have a video about socket programming using c++?

uanbu
Автор

а зачем float? например 320х240 разрешение предполагает что нам не нужны знаки после запятой. или я что то недопонял?

ticiusarakan
Автор

Parabéns xirú. Não conhecia teu canal, acabei chegando aqui pelo Akita. Muito bom. Inscrito e fortalecendo a comunidade.

wakeupneo