Given task is to implement a class in C++ which behaves just like the Vector class.
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. In vectors, data is inserted at the end. Inserting at the end takes differential time, as sometimes there may be a need of extending the array. Removing the last element takes only constant time because no resizing happens. Inserting and erasing at the beginning or in the middle is linear in time.
Certain functions associated with the Vector that we will implement are:
- void push(int data): This function takes one element and inserts it at the last. Amortized time complexity is O(1).
- void push(int data, int index): It inserts data at the specified index. Time complexity is O(1).
- int get(int index): It is used to get the element at the specified index. Time complexity is O(1).
- void pop(): It deletes the last element. Time complexity is O(1).
- int size(): It returns the size of the vector i.e, number of elements in the vector. Time complexity is O(1).
- int getcapacity(): It returns the capacity of the vector. Time complexity is O(1).
- void print(): It is used to print array elements. Time complexity is O(N), where N is the size of the vector.
Below is the implementation of our own Vector class.
Vector size : 5 Vector capacity : 8 Vector elements : 10 20 30 40 50 After updating 1st index Vector elements : 10 100 30 40 50 Element at 1st index : 100 After deleting last element Vector size : 4 Vector capacity : 8 Vector elements : 10 100 30 40
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Using class to implement Vector Quantities in C++
- How to implement our own Dynamic Array class in Java?
- Program to create Custom Vector Class in C++
- How to flatten a Vector of Vectors or 2D Vector in C++
- vector::front() and vector::back() in C++ STL
- vector::empty() and vector::size() in C++ STL
- vector::crend() & vector::crbegin() with example
- vector :: cbegin() and vector :: cend() in C++ STL
- vector::push_back() and vector::pop_back() in C++ STL
- vector::at() and vector::swap() in C++ STL
- vector::begin() and vector::end() in C++ STL
- Difference between Base class and Derived class in C++
- How to implement Min Heap using STL?
- Implement Your Own sizeof
- Implement your own itoa()
- Implement two stacks in an array
- Program to implement t-test
- How to convert a class to another class type in C++?
- How to implement user defined Shared Pointers in C++
- Implement your own tail (Read last n lines of a huge file)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.