C++ 如何重载运算符“

C++ 如何重载运算符“,c++,class,oop,vector,C++,Class,Oop,Vector,包括 #pragma once 向量 class Vector { public: Vector(); // Default constructor Vector(const Vector& rhs); // copy constructor Vector(int elements, int value = 0); Vector(const std::initializer_list<int>& list); ~Vector(); // destructor v

包括

#pragma once
向量

class Vector {
public:
Vector(); // Default constructor
Vector(const Vector& rhs); // copy constructor
Vector(int elements, int value = 0);
Vector(const std::initializer_list<int>& list);

~Vector(); // destructor

void PushBack(int value); // Add an element to the end of the vector.
void PushBackChar(int value); // Add an element to the end of the 
若Vector只存储整数,那个么您只能输出整数。我想你们的讲师要求你们有一个Vector_int类和一个Vector_char类,分别保存int和char,并且实现基本相同

旁白:我建议检查一下现有的实现,并将int的所有用法(索引或大小)更改为std::size\t,以便在复制char的实现时,可以在文本编辑器中执行全部替换


我希望本练习的学习结果是,这就是模板存在的原因

您需要实现vector类来支持CHAR。向量机目前是如何实现的?顺便问一下,你为什么要这样做而不使用std::vector?任务是我需要从头开始创建我的vector类,而不使用标准vector类的实用程序和C++的模板功能。如果不共享vector类的任何信息,我们将无法帮到你哈哈。你说得对,我们被要求重新发明自行车。也许是好的,也许不是,我不知道,但这仍然是一种体验
class Vector {
public:
Vector(); // Default constructor
Vector(const Vector& rhs); // copy constructor
Vector(int elements, int value = 0);
Vector(const std::initializer_list<int>& list);

~Vector(); // destructor

void PushBack(int value); // Add an element to the end of the vector.
void PushBackChar(int value); // Add an element to the end of the 
void PopBack(); // Deletes the element at the end of the vector.

bool Empty() const; // Tests if the vector container is empty.
int Size() const; // Returns the number of elements in the vector.
int Capacity() const; // Returns the number of elements that the vector could contain without allocating more storage.

bool operator==(const Vector& right_hand_side) const;
bool operator!=(const Vector& right_hand_side) const;

friend std::ostream& operator <<(std::ostream& ostr, const Vector& rhs);

Vector& operator=(const Vector& rhs);

int& operator[](int index);
int& At(int index); // Returns a reference to the element at a specified location in the vector.
int& Front(); // Returns a reference to the first element in a vector.
int& Back(); // Returns a reference to the last element of the vector.

void Insert(int index, int value); // Inserts an element or many elements into the vector at a specified position.
void Erase(int index);  // Removes an element or a range of elements in a vector from specified positions.
void Clear(); // Erases the elements of the vector.
private:
int size;
int capacity;
int* array;
char* char_array;
};