user3452963
user3452963

Reputation: 117

unsigned int vector c++

So I have a vector template class that looks like this:

#ifndef Vector_H
#define Vector_h

#include <iostream>
#include <assert.h>

using namespace std;

template <class T>
class Vector
{

public:
//constructor anddestructor
    Vector(unsigned int numberOfElements);
    virtual ~Vector();

    //overloading the [] operator
    T & operator [] (unsigned int index) const;

    //length
    unsigned int length() const;

private:
    T *     data; //the elements in the vector
    unsigned int size; 

};

In another class, I use the template class and create a vector. I want to read in lines from a .csv file and breakdown the line (i.e customer name, ID, age and gender) and put it into a vector.

Do I need to add an add function in my template class to add values into the vector? Am I going about this all wrong? Thanks for any help.

Upvotes: 1

Views: 1341

Answers (1)

Floris Velleman
Floris Velleman

Reputation: 4888

If you know the amount of elements you would add to the vector then that is not needed for your use case. You could just initialize the vector with that size and then use operator [] to modify the content. But a push_back-like function will be very usefull.

Am I going about this all wrong?

Assuming this is for learning purposes not really. If it is not: Yes, use std::vector<T>.

Upvotes: 1

Related Questions