Issue 8 : Implement Vector Class
Task Description:
This is the final issue of vector and this will be a long one but after this, you would have developed complete understanding of vectors. In this issue, you have to implement your own complete vector class in which you will have to write a total of 13 Functions.
Target File:
For this issue, you have to copy all the code from:
dsa/_examples/vector.h
And paste this code inside:
dsa/vector.h
File Structure:
Your file will look like this:
/*
This is a header file so dont write a main function.
Just create another main.cpp for testing your vector
class and include this header file in your main.cpp
by using following include preprocessor directive:
#include"vector.h"
Note: You must use " " instead of < > because this is
a user-defined header file. And also main.cpp must be
in the same folder as this file for it to work. When
you are done testing, delete your main.cpp before you
push your vector.h to github.
*/
// Replace "yourName" with your own name
class vector_yourName {
private:
int *ptr;
int cap;
int len;
public:
// Your Default+Parameterized Constructor Here
// Your Copy Constructor Here
// Your push_back Here
// Your resize Here
// Your pop_back Here
// Your back Here
// Your begin Here
// Your end Here
// Your operator[] Here
// Your size Here
// Your capacity Here
// Your empty Here
// Your Destructor Here
};
Default & Parameterized Constructor:
This will take an argument "int cap" as argument which will be the capacity of your vector (make sure cap is non-negative, if it is negative then make it positive). If user does not pass a value then cap should be set to 1 by default. The constructor should declare a dynamic array of cap size using the provided "int *ptr" and set the current number of elements "len" to 0. Note: Default & Parameterized Constructors shall be combined into one Constructor.
Copy Constructor( );
This is neccessary to make because vectors are using dynamic memory "Heap" and therefore we cannot afford to have shallow copies so write a deep copy constructor.
Push_back( );
Push_back function is going to take an "int val" as argument and insert that value into the vector if is already is not full (len==cap), if the vector is full the resize function will be called first to double the size of vector and then val will be inserted. In either case, len shall be increased by 1.
Resize( );
Resize function will be called inside push_back function when the vector is full and we need to insert a new value, resize will double the size of vector to make room for the new value to be inserted.
Pop_back( );
Pop_back function will remove the last value from the vector by doing a simple len--; if len is non-zero, otherwise it will just return without doing anything.
Back( );
Back function is a const getter function which will return the last inserted element in the vector (if vector is not empty). If vector is empty then execute the following line in your function:
throw std::runtime_error("Vector is empty!");
Begin( );
Begin function is a const getter function which will return the pointer to the starting address of vector.
End( );
End function is a const getter function which returns the pointer to the address after the last address of vector.
Operator [ ] ( );
You have to overload the [] operator for your vector which will take an "int index" in argument and if that index is inside the valid range of vector then it should return the value at that index. Return type of this function will be "int&". If the index is not inside the valid range of vector then execute this line in your function:
throw std::runtime_error("Invalid Index!");
Size( );
Size function is a const getter function which will return the current number of elements in the vector.
Capacity( );
Capacity is a const getter function which will return the capacity of the vector.
Empty( );
Empty function is a const boolean function which will return true if vector is empty and false if vector is not empty
Destructor( );
Destructor will just delete the "ptr" dynamic array to release the memory.
Guidelines:
You are only supposed to write your code inside the "vector_yourName class" in vector.h file. Don't write a "main function" inside the vector.h file, just create another main.cpp and include your vector.h in that main.cpp and write your main function in main.cpp to test your vector. When you have tested all the functions of your vector then delete main.cpp and main.exe before pushing vector.h to your github.
Important Note:
- Do Not Submit Ai Generated Codes!
- Make sure your functions are tested and working before you make your PR.
- Do not rename the vector.h file to anything else.
- Before submitting your PR, make sure you delete the main.cpp & main.exe, leave only vector.h file.
- Do not read other's vectors in their PR, just make sure that they aren't pushing any executables and approve the PR.
Issue 8 : Implement Vector Class
Task Description:
This is the final issue of vector and this will be a long one but after this, you would have developed complete understanding of vectors. In this issue, you have to implement your own complete vector class in which you will have to write a total of 13 Functions.
Target File:
For this issue, you have to copy all the code from:
dsa/_examples/vector.hAnd paste this code inside:
dsa/vector.hFile Structure:
Your file will look like this:
Default & Parameterized Constructor:
This will take an argument "int cap" as argument which will be the capacity of your vector (make sure cap is non-negative, if it is negative then make it positive). If user does not pass a value then cap should be set to 1 by default. The constructor should declare a dynamic array of cap size using the provided "int *ptr" and set the current number of elements "len" to 0. Note: Default & Parameterized Constructors shall be combined into one Constructor.
Copy Constructor( );
This is neccessary to make because vectors are using dynamic memory "Heap" and therefore we cannot afford to have shallow copies so write a deep copy constructor.
Push_back( );
Push_back function is going to take an "int val" as argument and insert that value into the vector if is already is not full (len==cap), if the vector is full the resize function will be called first to double the size of vector and then val will be inserted. In either case, len shall be increased by 1.
Resize( );
Resize function will be called inside push_back function when the vector is full and we need to insert a new value, resize will double the size of vector to make room for the new value to be inserted.
Pop_back( );
Pop_back function will remove the last value from the vector by doing a simple len--; if len is non-zero, otherwise it will just return without doing anything.
Back( );
Back function is a const getter function which will return the last inserted element in the vector (if vector is not empty). If vector is empty then execute the following line in your function:
Begin( );
Begin function is a const getter function which will return the pointer to the starting address of vector.
End( );
End function is a const getter function which returns the pointer to the address after the last address of vector.
Operator [ ] ( );
You have to overload the [] operator for your vector which will take an "int index" in argument and if that index is inside the valid range of vector then it should return the value at that index. Return type of this function will be "int&". If the index is not inside the valid range of vector then execute this line in your function:
Size( );
Size function is a const getter function which will return the current number of elements in the vector.
Capacity( );
Capacity is a const getter function which will return the capacity of the vector.
Empty( );
Empty function is a const boolean function which will return true if vector is empty and false if vector is not empty
Destructor( );
Destructor will just delete the "ptr" dynamic array to release the memory.
Guidelines:
You are only supposed to write your code inside the "vector_yourName class" in vector.h file. Don't write a "main function" inside the vector.h file, just create another main.cpp and include your vector.h in that main.cpp and write your main function in main.cpp to test your vector. When you have tested all the functions of your vector then delete main.cpp and main.exe before pushing vector.h to your github.
Important Note: