C++,需要帮助理解向量类中使用指针的一些构造函数和函数
大家好;
我必须开发一个 C++ 类库,其中包含科学计算数值技术的集合。该库应该实现 Vector 类(使用指针),并具有头文件“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 */
我是 C++ 的初学者,对上面代码中的一些构造函数和函数的理解有些困惑。
我的问题是:
1-以下构造函数的概念是什么?
//initialize to array a
CVector(int n, const T *a);
我的意思是如何将向量初始化为数组 a?
2- 复制构造函数和赋值构造函数有什么区别?
//copy constractor
CVector(const CVector &rhs);
//assignment
CVector & operator=(const CVector &rhs);
3-我知道这个函数是返回向量的第 i 个元素:
//i'th element
inline T & operator[](const int i);
但是它和这个有什么区别:
inline const T & operator[](const int i) const;
我需要理解这个概念,以便知道如何在 .cpp 文件中实现它们以及如何在我的 main 中调用它们。如果你能帮助我,我会很高兴。
此致;
Greetings All;
I have to develop a C++ class library comprising a collection of numerical techniques for scientific computing. The library should implement Vector class (using pointers) with some basic functionality stated in a header file "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.
My questions are:
1- What is the concept of the following constructor?
//initialize to array a
CVector(int n, const T *a);
I mean how to initialize a vector to an array a?
2- What is the difference between the copy constructor and the assignment one?
//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:
//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. I'll be glad if you help me.
Best regards;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Q1:该构造函数可用于用从 a 开始的数组的 n 个元素的内容填充向量。
例如:
Q2:第一个是复制构造函数,第二个是赋值运算符。复制构造函数用于将值复制到函数参数中、从函数返回值或初始化变量。
例如,复制构造函数用于以下情况:
赋值用于以下情况:
Q3。第一个可以用在作业的左侧。当应用于 const 对象时,使用 const 版本。
Q1: This constructor can be used to fill the vector with the contents of the n elements of the array starting at a.
For example:
Q2: The first is a copy constructor, the second is an assignment operator. The copy constructor is used to copy values into a function parameter, to return a value from a function, or to initialize a variable.
For example, the copy constructor is used for these:
And the assignment is used for this:
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.
1) 将您的成员设置为:
v = new T[n]; nn = n;
并复制元素:
for (int i = 0; i != n; ++i) v[i] = a[i];
2) 复制赋值是指当您已经拥有一个对象时,并想为其分配不同的值。复制构造函数是指您想要使用现有对象的值创建一个新对象。
3)在c++中,有一个const函数的概念:如果你在一个const对象上调用该函数;说:CVector常量& x = ...; x[3] = 3; 不起作用,因为
x
是 const。但要想让这个方法不起作用,operator[]
需要向您的内部返回一个const &
。仅供参考:如果
x
是非常量,则x[3]
的类型是T &
,因为x[3]
的类型是非常量版本使用>运算符[]。但是,如果x
是 const,则x[3]
的类型是const T &
,因为operator []< 的 const 版本使用/代码>。
1) set your members to:
v = new T[n]; nn = n;
and copy the elements:
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. Copy-constructor is when you want to create a new object with values from an existing one.
3) In c++ there is a concept of const functions: if you call the function on a const object; say:
CVector<int> const& x = ...; x[3] = 3;
won't work, sincex
is const. But for this to not work, theoperator[]
needs to return aconst &
to your internals.FYI: if
x
is non-const, the type ofx[3]
isT &
because the non-const version ofoperator []
is used. Ifx
is const however, the type ofx[3]
isconst T &
because const version ofoperator []
is used.好吧,我不是 C++ 专家,所以希望更有能力的人能插话……但这是我在这个主题上的 0.02 美元。
迭代数组,将每个元素添加到向量中。我假设 n 是数组中元素的数量?
复制构造函数和赋值构造函数之间的区别可能是语义上的。它们对向量的内部结构具有相同的效果,但可以在不同的情况下使用。
这只是一个猜测,但我认为可变性就是区别。第二个函数返回一个不可变的 T,这意味着它无法更改。第一个函数返回一个可变的(可更改的)T。
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.
Iterate over the array, adding each element to the vector. I am assuming that n is the number of elements in the array?
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.
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. The first function returns a mutable (changeable) T.