简体   繁体   English

无符号int向量c ++

[英]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 (ie customer name, ID, age and gender) and put it into a vector. 我想从.csv文件中读取行并细分行(即客户名称,ID,年龄和性别),然后将其放入向量中。

Do I need to add an add function in my template class to add values into the vector? 我是否需要在模板类中添加add函数以将值添加到向量中? Am I going about this all wrong? 我要解决所有这些错误吗? Thanks for any help. 谢谢你的帮助。

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. 但是类似push_back的函数将非常有用。

Am I going about this all wrong? 我要解决所有这些错误吗?

Assuming this is for learning purposes not really. 假设这不是真正出于学习目的。 If it is not: Yes, use std::vector<T> . 如果不是:是,请使用std::vector<T>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM