已知一个程序: void main(){Vector v(1,23);
cout<<” Vector v:”<<v<<end1;}运行后输出结果如下:
Vector(1,2,3) is created! Vector v: (1,2,3)
Vector(1,2,3) is destroyed!
请根据程序运行结果要求完成类 Vector 的定义及实现。
#include <iostream>
using namespace std;
class Vector {
friend ostream& operator<<(ostream& os, const Vector& v);
public:
Vector(int a = 0, int b = 0, int c = 0) : x(a), y(b), z(c) {
cout << "Vector(" << x << "," << y << "," << z << ") is created!" << endl;
}
~Vector() {
cout << "Vector(" << x << "," << y << "," << z << ") is destroyed!" << endl;
}
private:
int x, y, z;
};
ostream& operator<<(ostream& os, const Vector& v) {
os << "(" << v.x << "," << v.y << "," << v.z << ")";
return os;
}
int main() {
Vector v(1, 2, 3);
cout << "Vector v:" << v << endl;
return 0;
}
运行效果如下:
解释一下, 写的比较着急 , 代码没加注释, 大差不差, 简单分析下 , 如有帮助给个采纳谢谢
主函数中定义了 Vector 对象 v,使用此对象的坐标初始化,并使用 cout 语句输出该对象的坐标,销毁对象时也会自动调用析构函数输出销毁提示信息。
代码如下 :
#include <iostream>
using namespace std;
class Vector {
private:
int x;
int y;
int z;
public:
Vector(int x, int y, int z) {
this->x = x;
this->y = y;
this->z = z;
cout << "Vector(" << x << ", " << y << ", " << z << ") is created!" << endl;
}
friend ostream& operator<<(ostream& os, const Vector& v) {
os << "(" << v.x << ", " << v.y << ", " << v.z << ")";
return os;
}
~Vector() {
cout << "Vector(" << x << ", " << y << ", " << z << ") is destroyed!" << endl;
}
};
int main() {
Vector v(1, 2, 3);
cout << "Vector v: " << v << endl;
return 0;
}
是的,请根据程序运行结果,帮忙定义和实现一个类 Vector。参考资料中已经提到了 Vector 类的定义和一些常用函数,可以基于这些内容进行实现。我们可以先确定 Vector 类的基本功能:添加、删除、查找、遍历、清空等。具体步骤如下:
template <typename T>
class Vector{
private:
T* data; //指向存储元素的数组地址
int size; //当前动态数组的长度
int capacity; //当前动态数组的容量
void resize(int new_capacity); //调整容量
public:
Vector(int capacity=10); //构造函数,capacity为初始容量
Vector(const Vector& other); //拷贝构造函数
~Vector(); //析构函数
int Size() const; //返回当前动态数组的长度
bool Empty() const; //判断动态数组是否为空
T& operator[](int index); //将运算符重载为下标操作符
Vector& operator=(const Vector& other); //将运算符重载为赋值操作符
void PushBack(const T& element); //在数组尾部添加元素
void PopBack(); //删除数组尾部元素
int Find(const T& element) const; //查找元素
void Clear(); //清空数组
void Traverse(void (*visit)(const T& element)); //遍历数组
};
template <typename T>
Vector<T>::Vector(int capacity){
data = new T[capacity];
size=0;
this->capacity=capacity;
}
template <typename T>
Vector<T>::~Vector(){
delete []data;
}
template <typename T>
Vector<T>::Vector(const Vector& other){
data = new T[other.capacity];
capacity = other.capacity;
for(int i=0;i<other.size;i++){
data[i] = other.data[i];
}
size = other.size;
}
template <typename T>
void Vector<T>::resize(int new_capacity){
T* new_data = new T[new_capacity];
for(int i=0;i<size;i++){
new_data[i] = data[i];
}
delete []data;
data = new_data;
capacity = new_capacity;
}
template<typename T>
T& Vector<T>::operator[](int index){
assert(index>=0 and index<size);
return data[index];
}
template <typename T>
Vector<T>& Vector<T>::operator=(const Vector& other){
if(this!=&other){
delete []data;
data = new T[other.capacity];
capacity = other.capacity;
for(int i=0;i<other.size;i++){
data[i] = other.data[i];
}
size = other.size;
}
return *this;
}
template<typename T>
void Vector<T>::PushBack(const T& element){
if(size==capacity){
resize(2*capacity);
}
data[size++] = element;
}
template<typename T>
void Vector<T>::PopBack(){
assert(size>0);
size--;
if(size<=capacity/4){
resize(capacity/2);
}
}
template<typename T>
int Vector<T>::Find(const T& element) const{
for(int i=0;i<size;i++){
if(data[i]==element){
return i;
}
}
return -1;
}
template<typename T>
void Vector<T>::Clear(){
size = 0;
resize(10);
}
template<typename T>
void Vector<T>::Traverse(void (*visit)(const T& element)){
for(int i=0;i<size;i++){
visit(data[i]);
}
}
以上为 Vector 类的具体实现过程,可能还有一些小问题需要调整和完善。在需要使用 Vector 类的程序中,只需要 include 头文件即可。如下:
#include "Vector.hpp"
#include <iostream>
using namespace std;
void print_func(const int& element){
cout<<element<<" ";
}
int main(){
Vector<int> vec(2);
vec.PushBack(1);
vec.PushBack(2);
vec.PushBack(3);
vec.PushBack(4);
vec[1] = 5;
vec.Traverse(print_func);
cout<<"\n"<<vec.Find(5)<<"\n";
vec.Clear();
return 0;
}