简体   繁体   English

C++,需要帮助理解向量class中使用指针的一些构造函数和函数

[英]C++,Need help to understand some constructors and functions in a vector class using pointers

Greetings All;大家好;

I have to develop a C++ class library comprising a collection of numerical techniques for scientific computing.我必须开发一个 C++ class 库,其中包含一系列用于科学计算的数值技术。 The library should implement Vector class (using pointers) with some basic functionality stated in a header file "Vector.h".该库应实现 Vector class(使用指针),并具有 header 文件“Vector.h”中所述的一些基本功能。

#ifndef VECTOR_H
#define VECTOR_H

template <class T>
class CVector {
private:
    int nn; //size of array
    T *v;   //pointer to array of data

public:

    //Default constractor
   CVector();

    //zero based array
    CVector(int n);

    //initialize to constant of value a
    CVector(int n, const T &a);

    //initialize to array a
    CVector(int n, const T *a);

    //copy constractor
    CVector(const CVector &rhs);

    //assignment
    CVector & operator=(const CVector &rhs);

    //i'th element
    inline T & operator[](const int i);

    inline const T & operator[](const int i) const;

    inline int size() const;

    //resize (contents not preserved)
    void resize(int newn);

    //resize and assign a constant value
    void assign(int newn, const T &a);

    //deconstractor
    ~CVector();

};

#endif  /* VECTOR_H */

I am a beginner in C++ and I have some confusion to understand some constructors and functions in the above code.我是 C++ 的初学者,我对理解上面代码中的一些构造函数和函数有些困惑。

My questions are:我的问题是:

1- What is the concept of the following constructor? 1-以下构造函数的概念是什么?

    //initialize to array a
    CVector(int n, const T *a);

I mean how to initialize a vector to an array a?我的意思是如何将向量初始化为数组a?

2- What is the difference between the copy constructor and the assignment one? 2-复制构造函数和赋值之间有什么区别?

    //copy constractor
    CVector(const CVector &rhs);

    //assignment
    CVector & operator=(const CVector &rhs);

3- I know that this function is to return the ith element of the vector: 3-我知道这个 function 是返回向量的第 i 个元素:

    //i'th element
    inline T & operator[](const int i);

but what is the difference between it and this one:但它和这个有什么区别:

    inline const T & operator[](const int i) const;

I need to understand the concept in order to know how to implement them in the.cpp file and how to call them in my main.我需要理解这个概念才能知道如何在.cpp 文件中实现它们以及如何在我的 main.cpp 文件中调用它们。 I'll be glad if you help me.如果你能帮助我,我会很高兴的。

Best regards;此致;

Q1: This constructor can be used to fill the vector with the contents of the n elements of the array starting at a. Q1:此构造函数可用于用数组从 a 开始的 n 个元素的内容填充向量。

For example:例如:

   float a[42] = { 31, 41, 59 };
   CVector<float> v( 3, a );

Q2: The first is a copy constructor, the second is an assignment operator. Q2:第一个是拷贝构造函数,第二个是赋值运算符。 The copy constructor is used to copy values into a function parameter, to return a value from a function, or to initialize a variable.复制构造函数用于将值复制到 function 参数中,从 function 返回值,或初始化变量。

For example, the copy constructor is used for these:例如,复制构造函数用于这些:

CVector<float> foo( CVector<float> v ) { ... }

...
CVector<float> v1;
CVector<float> v2 = foo( v1 ); // Copy constructor used to pass in v1, and to return v2
CVector<float> v3 = v1; // Copy constructor used to copy v1 to v2.

And the assignment is used for this:并且分配用于此:

CVector<float> v4;
v4 = v1;

Q3. Q3。 The first can be used on the left-hand-side of an assignment.第一个可以用在作业的左侧。 The const version is used when applied to a const object. const 版本在应用于 const object 时使用。

void bar( const float & fval ) { ... }
...
CVector<float> v1( 3, a );
v1[0] = 42;   // Non-const operator[]

const CVector<float> v2 = v1; 
float value = v2[0];  // Const operator[]
bar( v2[0] ); // Const operator[]

1) set your members to: v = new T[n]; nn = n; 1) 将您的成员设置为: v = new T[n]; nn = n; v = new T[n]; nn = n; and copy the elements: for (int i = 0; i;= n; ++i) v[i] = a[i];并复制元素: for (int i = 0; i;= n; ++i) v[i] = a[i];

2) Copy-assignment is when you already have an object, and want to assign a different value to it. 2) 复制分配是指您已经拥有一个 object,并且想要为其分配不同的值。 Copy-constructor is when you want to create a new object with values from an existing one.复制构造函数是当您想要使用现有值创建一个新的 object 时。

3) In c++ there is a concept of const functions: if you call the function on a const object; 3) 在 c++ 中有一个 const 函数的概念:如果在 const object 上调用 function; say: CVector<int> const& x =...; x[3] = 3;说: CVector<int> const& x =...; x[3] = 3; CVector<int> const& x =...; x[3] = 3; won't work, since x is const.行不通,因为x是 const。 But for this to not work, the operator[] needs to return a const & to your internals.但是为了让它不起作用, operator[]需要返回一个const &到你的内部。
FYI: if x is non-const, the type of x[3] is T & because the non-const version of operator [] is used.仅供参考:如果x是非常量,则x[3]的类型是T &因为使用了非常量版本的operator [] If x is const however, the type of x[3] is const T & because const version of operator [] is used.但是,如果x是 const,则x[3]的类型是const T &因为使用了 const 版本的operator []

Ok, I'm not a C++ guru, so hopefully someone a little more competent will chime in...but here is my $.02 on the subject.好的,我不是 C++ 大师,所以希望有人能更胜一筹……但这是我在这个问题上的 0.02 美元。

  1. Iterate over the array, adding each element to the vector.遍历数组,将每个元素添加到向量中。 I am assuming that n is the number of elements in the array?我假设 n 是数组中的元素数?

  2. The difference between the copy constructor and the assignment one is probably semantic.复制构造函数和赋值构造函数之间的区别可能是语义上的。 They will have the same effect on the internal structure of the vector, but can be used in different situations.它们对向量的内部结构会产生相同的影响,但可以在不同的情况下使用。

  3. This is just a guess, but I am thinking mutability is the difference.这只是一个猜测,但我认为可变性是区别。 The second function returns an immutable T, meaning that it cannot be changed.第二个 function 返回一个不可变的 T,这意味着它不能更改。 The first function returns a mutable (changeable) T.第一个 function 返回一个可变(可变)T。

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

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