Using vector class without header file in c++

In this tutorial, we will learn how to use a vector class without a header file in c++. Vector container implements array with fast random access with the capability to resize automatically when adding elements. In this tutorial, we will learn that we can use vector without a header file too by just implementing a class and declaring its data members and functions.

We generally use a vector with the help of an STL(standard template library) library. What we will learn in this article is going to be something different.

How to use vector without a header file in c++

Vectors are sequence containers representing arrays that can change in their size. A vector class is defined in #include<vector> header file. It has the following functions defined in it:

  1. PUSH_BACK-It adds the elements in a vector at the end and resizes it automatically.
  2. POP_BACK-It pops the last element. For popping elements at rest of the indexes, we need an iterator.
  3. EMPTY- It returns true if the vector is empty
  4. GET SIZE-It computes the current size of the vector.
  5. GETMAXSIZE-It computes the maximum size to which the vector has been resized.
  6. AT-It returns the vector at that particular index.
  7. PRINT-It prints all the elements of a vector.
  8. ()-This operator is overloaded to append a string at the end of its argument string.
  9. []-This operator is overloaded to return the element at the index passed as an argument.

In comparison to other containers, vectors are very efficient in accessing its elements, efficient adding and removing elements from the end. For our operations that involve inserting or removing elements at positions other than the end, they perform worse than the others.

These above mentioned are the functionalities of a vector container that we have implemented in our vector class. Here is the code to implement it:

Also refer to :

Reverse a vector in c++




 

#include <iostream>
using namespace std;

//Let us define a Vector Class(User defined Vector)
class Vector{
    //Data Members and some functions
    int *arr;
    int cs;
    int maxSize;

public:
    Vector(int defaultSize=4){
        maxSize = defaultSize;
        cs = 0;
        arr = new int[maxSize];
    }

   void operator()(string s)
   {
cout<<s<<"is an awesome place";
   }
   void operator[](int i)
   {
       cout<< arr[i];
   }

    void push_back(int data){
        if(cs==maxSize){
            //Doubling Operations
            int *oldArr = arr;
            arr = new int[2*maxSize];
            maxSize = 2*maxSize;

            for(int i=0;i<cs;i++){
                arr[i] = oldArr[i];
            }
            //Delete the oldArr
            delete [] oldArr;
        }
        arr[cs] = data;
        cs++;
    }
    bool empty(){
        return cs==0;
    }
    int getSize(){
        return cs;
    }
    int getMaxSize(){
        return maxSize;
    }
    void pop_back(){
        if(!empty()){
            cs--;
        }
    }
    void print(){
        for(int i=0;i<cs;i++){
            cout<<arr[i]<<",";
        }
    }
    int at(int i){
        return arr[i];
    }

};

int main() {
    Vector v(1000);
    v("dehradun ");
    cout<<endl;
    v[4];

    for(int i=0;i<=3;i++){
        v.push_back(i*i);
    }
    cout<<v.getMaxSize()<<endl;
    v.push_back(7);
    cout<<v.getMaxSize()<<endl;
    v.print();


}

The output will be:

dehradun is an awesome place
01000
1000
0,1,4,9,7



Leave a Reply

Your email address will not be published. Required fields are marked *