-3

I am trying to buld the vector library . The code looks like this

#include <iostream>
#include <string>

namespace std
{
   template<typename T>
   class vector
   {
      private:
      T* value;
      T* temp;
      int arraySize{1};

      public:
      vector()
      {
        value=NULL;
        temp=NULL;
      }

      int size()
      {
          return arraySize;
      }

      void push(T val)
      {
        if(value==NULL)
        {
          value=new T[arraySize];
          value[arraySize-1]=val;
        }
        else if(value!=NULL)
        {
          arraySize=arraySize+1;
          temp=new T[arraySize];
          int lastIndex=arraySize-1;
          for(int j=0;j<lastIndex;j++)
            {
              temp[j]=value[j];
            }
          temp[lastIndex]=val;
          delete[] value;
          value=temp;
          temp=NULL;
        }
      }

      T pop()
      {
        int popIndex=arraySize-1;
        T t = value[popIndex];
        arraySize=arraySize-1;
        if(arraySize>=0)
        {
          temp=new T[arraySize];
          for(int j=0;j<popIndex;j++)
            {
              temp[j]=value[j];
            }
          delete[] value;
          value=temp;
          temp=NULL;
          return t;
        }
        else if(arraySize<0)
        {
          std::cout<<"Memory underflow\n";  
          reset();
        }
      }

      T operator[](int index)const
      {
        return value[index];  
      }

      T front()const
      {
          return (value[arraySize-1]);
      }

      T back()const
      {
          return (value[0]);
      }

      private:
      void reset()
      {
        arraySize=1;
        vector();
      }
   };
}

int main() 
{
  std::vector<std::string> v;

  v.push("1");
  v.push("2");
  v.push("3");

  std::cout<<"the element is : "<<v[1]<<"\n";

  std::cout<<"front element is : "<<v.front()<<"\nback(last) element is : "<<v.back()<<"\n";

  std::cout<<"size : "<<v.size()<<"\n";
  std::cout<<v.pop()<<"\n";
  std::cout<<v.pop()<<"\n";
  std::cout<<"size : "<<v.size()<<"\n";

  v.push("4");

  std::cout<<"size : "<<v.size()<<"\n";
  std::cout<<v.pop()<<"\n";
  std::cout<<"size : "<<v.size()<<"\n";
}

I have overloaded this [] operator for v[1] but i want to write as this for example
v[2]="2";

How should I do it Please help . And please suggest some changes in code if they are to be done

I tried to look for overloading of = operator but it tells that you can take only one argument

13
  • 4
    How about T& operator[](int index) { return value[index]; }? Commented Jun 15, 2023 at 11:37
  • 9
    you should not add things to namespace std. Even if you do not include vector directly others headers might do, or might have a forward declaration. Just make it my::vector and all is fine Commented Jun 15, 2023 at 11:41
  • And just curious why do you initialize array size by 1? That might get you into of-by-one bugs later. It is also more customary to maintain sizes using std::size (an unsigned type) so you cannot accidentally work with negatively sized data. Commented Jun 15, 2023 at 11:41
  • 4
    You don't need to keep the temp around all the time. It is much cheaper to just create it inside the functions that need it. And also, the vector() inside reset doesn't clear this vector, but a new temporary vector that then immediately goes away again. Commented Jun 15, 2023 at 11:49
  • 1
    v[2] = "2"; has nothing to do with overloading vector::operator=. vector::operator= is for assigning vectors, when you write v[2]="2"; you are assigning a string not a vector. The problem is that your vector::operator[] is returning the wrong type. It should return T& not T. Then you will be able to writev[2]="2";. Commented Jun 15, 2023 at 11:54

1 Answer 1

0

Lets break this down to a simpler example, because frankly, your code has too many issues to be addressed in a single answer. The comments mention some of them, you should take them serious, because your vector is very broken. Lets consider only implementation of a method that returns something that you can assign to. In other words, we want to write this code:

int main() {
     foo f;
     f.set("123");       // sets a member
     f.get() = "42";     // gets something that can be used to 
                         // assign to the member
     std::cout << f.get() << "\n";
}

And output should be 42.

We need a default constructor. We need a push method to set a string. A single string as member is sufficient. To get f.get() = "42"; working you do not need to implement an operator=. What is assigned is a string. std::string already has an operator=. However, get() should return a reference to the internal string. It should not return a copy of the internal string, because modifying that copy will have no effect on the member. Return a reference.

struct foo {
    std::string data;

    void set(const std::string& x) { data = x; }

    using reference = std::string&;

    reference get() { return data; }
    
};

Live Demo

For futher reading I recommend...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.