The document discusses the vector data structure. It notes limitations of built-in arrays in C++ and the need for a more flexible dynamic array. It proposes creating a Vector class with an internal array and size variable to allow flexible operations like insertion and removal. The document outlines thinking through the necessary data and operations, providing examples of member functions for operations like insertion, searching and sorting. It describes implementing a Vector class with get/set functions and additional operations like printing. The conclusion is that a basic dynamic array/vector data structure has been created.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
32 views9 pages
01 Vector
The document discusses the vector data structure. It notes limitations of built-in arrays in C++ and the need for a more flexible dynamic array. It proposes creating a Vector class with an internal array and size variable to allow flexible operations like insertion and removal. The document outlines thinking through the necessary data and operations, providing examples of member functions for operations like insertion, searching and sorting. It describes implementing a Vector class with get/set functions and additional operations like printing. The conclusion is that a basic dynamic array/vector data structure has been created.
PhD from Simon Fraser University - Canada Bachelor / Msc from Cairo University - Egypt Ex-(Software Engineer / ICPC World Finalist) Array limitation ● A C++ built-in array is mainly limited by its initial size ○ Once size is determined, it is fixed ○ This means it is not flexible to insert/remove elements that affects the size ● In practice, we need something that is more memory flexible ○ Such as append (push_back), insert and delete ● In addition, we might want to support some operations. Examples ○ Search for an element, get the minimum element, sort content and so on ○ Definitely also we need to set and get values from the array (e.g. []) ● Let’s create such a dynamic array ○ In C++, we may it vector, similar to STL Vector class Thinking ● Let’s say we will create a class: Vector (capital V) ● We need to think about our data and operations ● For Data ○ Vector is just a dynamic flexible array ○ It is intuitive to have an array internally, but this time with pointers, e.g. int* ○ We also need to have a size for the current array! ⇒ int size ● Operations ○ Whatever an interesting operation, we just provide a member function to it! ○ E.g. insert function or search function Let’s code ● Create a class ● Add your data in private section ○ Let’s focus on integers ○ Array size ● Our constructor ○ Just create memory ● Destructor ○ To handle memory leak ○ Sometimes I might skip it, only for simplicity Get and Set ● In arrays, we can set/get data using [] ● Let’s provide similar functionalities Operations ● We can add more functions to match our needs ● E.g. print array ● E.g. find index of a value ○ Or -1 if doesn’t exist Usage ● We simply create our own object and start to use the functions Data Structure ● As you see, we are done with our first basic data structure ○ Vector is one of the basic but heavily used data structures ● We introduced data and some operations around it ● In next videos, we see a real need for such a data structure “Acquire knowledge and impart it to the people.”