Vector 实现的正确方法
Correct way of Vector Implementation
谁能告诉我它是否是矢量实现?如果没有,那么问题是我的代码有什么问题以及如何以正确的方式实现向量。请指导。
P.S: 我在这里只添加了部分代码。我的 PUSH 函数是否成功实现了 VECTOR。
template <class T>
class Vector
{
private:
T *input;
int top;
int capacity;
public:
Vector();
~Vector();
void push(T x);
};
template <class T>
Vector<T>::Vector()
{
top = -1;
capacity = 5;
input = new T[capacity];
}
template <class T>
Vector<T>::~Vector()
{
delete[]input;
}
template <class T>
void Vector<T>::push(T x)
{
if (top + 1 == capacity)
{
T *vec = new T[capacity*2];
for (int i = 0; i <= top; i++)
{
vec[i] = input[i];
}
delete[]input; //Avoiding memory leak
input = vec;
capacity *= 2;
}
top++;
input[top] = x;
}
我正在添加完整的可运行代码。它基本上是我编写的 STACK 代码。但我想知道的是在这段代码中我是否成功使用了 VECTOR CONCEPT 或 NOT。如果不是,什么是正确的 VECTOR 实现。
#include"iostream"
template <class T>
class Mystack
{
private:
T *input;
int top;
int capacity;
public:
Mystack();
~Mystack();
void push(T x);
T pop();
T topElement() const;
bool isEmpty() const;
void print();
};
template <class T>
Mystack<T>::Mystack()
{
top = -1;
capacity = 5;
input = new T[capacity];
}
template <class T>
Mystack<T>::~Mystack()
{
delete[]input;
}
template <class T>
void Mystack<T>::push(T x)
{
if (top + 1 == capacity)
{
T *vec = new T[capacity * 2];
for (int i = 0; i <= top; i++)
{
vec[i] = input[i];
}
delete[]input;
input = vec;
capacity *= capacity;
}
top++;
input[top] = x;
}
template <class T>
T Mystack<T>::pop()
{
if (isEmpty())
{
throw std::out_of_range("Stack Underflow");
}
else
{
std::cout << "The popped element is" << input[top];
return input[top--];
}
}
template <class T>
bool Mystack<T>::isEmpty() const
{
if (top == -1)
{
std::cout << "Is Empty" << std::endl;
return true;
}
else
{
std::cout << "Not Empty" << std::endl;
return false;
}
}
template <class T>
T Mystack<T>::topElement() const
{
if (top == -1)
{
throw std::out_of_range("No Element to Display");
}
else
{
std::cout << "The top element is : " << input[top];
return input[top];
}
}
template <class T>
void Mystack<T>::print()
{
for (int i = 0; i <= top; i++)
{
std::cout << input[i] << " ";
}
}
int main()
{
Mystack<int> s1;
Mystack<float> s2;
Mystack<char> s3;
int choice;
int int_elem;
float float_elem;
char char_elem;
std::cout << "Enter the type of stack" << std::endl;
std::cout << "1. int" << std::endl;
std::cout << "2. float" << std::endl;
std::cout << "3. Char" << std::endl;
std::cin >> choice;
if (choice == 1)
{
int ch = 1;
while (ch > 0)
{
std::cout << "\n1. PUSH" << std::endl;
std::cout << "2. TOP" << std::endl;
std::cout << "3. IsEmpty" << std::endl;
std::cout << "4. POP" << std::endl;
std::cout << "5. EXIT" << std::endl;
std::cout << "6. Print" << std::endl;
std::cout << "Enter the choice" << std::endl;
std::cin >> ch;
switch (ch)
{
case 1:
std::cout << "Enter the number to be pushed" << std::endl;
std::cin >> int_elem;
s1.push(int_elem);
break;
case 2:
std::cout << "Get the TOP Element" << std::endl;
try
{
s1.topElement();
}
catch (std::out_of_range &oor)
{
std::cerr << "Out of Range error:" << oor.what() << std::endl;
}
break;
case 3:
std::cout << "Check Empty" << std::endl;
s1.isEmpty();
break;
case 4:
std::cout << "POP the element" << std::endl;
try
{
s1.pop();
}
catch (const std::out_of_range &oor)
{
std::cerr << "Out of Range error: " << oor.what() << '\n';
}
break;
case 5:
exit(0);
case 6:
s1.print();
break;
default:
std::cout << "Enter a valid input";
break;
}
}
}
else if (choice == 2)
{
int ch = 1;
while (ch > 0)
{
std::cout << "\n1. PUSH" << std::endl;
std::cout << "2. TOP" << std::endl;
std::cout << "3. IsEmpty" << std::endl;
std::cout << "4. POP" << std::endl;
std::cout << "5. EXIT" << std::endl;
std::cout << "6. Print" << std::endl;
std::cout << "Enter the choice" << std::endl;
std::cin >> ch;
switch (ch)
{
case 1:
std::cout << "Enter the number to be pushed" << std::endl;
std::cin >> float_elem;
s2.push(float_elem);
break;
case 2:
std::cout << "Get the TOP Element" << std::endl;
try
{
s2.topElement();
}
catch (std::out_of_range &oor)
{
std::cerr << "Out of Range error:" << oor.what() << std::endl;
}
break;
case 3:
std::cout << "Check Empty" << std::endl;
s2.isEmpty();
break;
case 4:
std::cout << "POP the element" << std::endl;
try
{
s2.pop();
}
catch (const std::out_of_range &oor)
{
std::cerr << "Out of Range error: " << oor.what() << '\n';
}
break;
case 5:
exit(0);
case 6:
s2.print();
break;
default:
std::cout << "Enter a valid input";
break;
}
}
}
else if (choice == 3)
{
int ch = 1;
while (ch > 0)
{
std::cout << "\n1. PUSH" << std::endl;
std::cout << "2. TOP" << std::endl;
std::cout << "3. IsEmpty" << std::endl;
std::cout << "4. POP" << std::endl;
std::cout << "5. EXIT" << std::endl;
std::cout << "6. Print" << std::endl;
std::cout << "Enter the choice" << std::endl;
std::cin >> ch;
switch (ch)
{
case 1:
std::cout << "Enter the number to be pushed" << std::endl;
std::cin >> char_elem;
s3.push(char_elem);
break;
case 2:
std::cout << "Get the TOP Element" << std::endl;
try
{
s3.topElement();
}
catch (std::out_of_range &oor)
{
std::cerr << "Out of Range error:" << oor.what() << std::endl;
}
break;
case 3:
std::cout << "Check Empty" << std::endl;
s3.isEmpty();
break;
case 4:
std::cout << "POP the element" << std::endl;
try
{
s3.pop();
}
catch (const std::out_of_range &oor)
{
std::cerr << "Out of Range error: " << oor.what() << '\n';
}
break;
case 5:
exit(0);
case 6:
s3.print();
break;
default:
std::cout << "Enter a valid input";
break;
}
}
}
else
std::cout << "Invalid Choice";
std::cin.get();
}
实现 VECTOR STL 中存在的所有标准函数。这应该会增强您对矢量的理解。并且有问题的代码不是 VECTOR 的正确实现,因为 Vector STL 提供了许多其他功能。
谁能告诉我它是否是矢量实现?如果没有,那么问题是我的代码有什么问题以及如何以正确的方式实现向量。请指导。 P.S: 我在这里只添加了部分代码。我的 PUSH 函数是否成功实现了 VECTOR。
template <class T>
class Vector
{
private:
T *input;
int top;
int capacity;
public:
Vector();
~Vector();
void push(T x);
};
template <class T>
Vector<T>::Vector()
{
top = -1;
capacity = 5;
input = new T[capacity];
}
template <class T>
Vector<T>::~Vector()
{
delete[]input;
}
template <class T>
void Vector<T>::push(T x)
{
if (top + 1 == capacity)
{
T *vec = new T[capacity*2];
for (int i = 0; i <= top; i++)
{
vec[i] = input[i];
}
delete[]input; //Avoiding memory leak
input = vec;
capacity *= 2;
}
top++;
input[top] = x;
}
我正在添加完整的可运行代码。它基本上是我编写的 STACK 代码。但我想知道的是在这段代码中我是否成功使用了 VECTOR CONCEPT 或 NOT。如果不是,什么是正确的 VECTOR 实现。
#include"iostream"
template <class T>
class Mystack
{
private:
T *input;
int top;
int capacity;
public:
Mystack();
~Mystack();
void push(T x);
T pop();
T topElement() const;
bool isEmpty() const;
void print();
};
template <class T>
Mystack<T>::Mystack()
{
top = -1;
capacity = 5;
input = new T[capacity];
}
template <class T>
Mystack<T>::~Mystack()
{
delete[]input;
}
template <class T>
void Mystack<T>::push(T x)
{
if (top + 1 == capacity)
{
T *vec = new T[capacity * 2];
for (int i = 0; i <= top; i++)
{
vec[i] = input[i];
}
delete[]input;
input = vec;
capacity *= capacity;
}
top++;
input[top] = x;
}
template <class T>
T Mystack<T>::pop()
{
if (isEmpty())
{
throw std::out_of_range("Stack Underflow");
}
else
{
std::cout << "The popped element is" << input[top];
return input[top--];
}
}
template <class T>
bool Mystack<T>::isEmpty() const
{
if (top == -1)
{
std::cout << "Is Empty" << std::endl;
return true;
}
else
{
std::cout << "Not Empty" << std::endl;
return false;
}
}
template <class T>
T Mystack<T>::topElement() const
{
if (top == -1)
{
throw std::out_of_range("No Element to Display");
}
else
{
std::cout << "The top element is : " << input[top];
return input[top];
}
}
template <class T>
void Mystack<T>::print()
{
for (int i = 0; i <= top; i++)
{
std::cout << input[i] << " ";
}
}
int main()
{
Mystack<int> s1;
Mystack<float> s2;
Mystack<char> s3;
int choice;
int int_elem;
float float_elem;
char char_elem;
std::cout << "Enter the type of stack" << std::endl;
std::cout << "1. int" << std::endl;
std::cout << "2. float" << std::endl;
std::cout << "3. Char" << std::endl;
std::cin >> choice;
if (choice == 1)
{
int ch = 1;
while (ch > 0)
{
std::cout << "\n1. PUSH" << std::endl;
std::cout << "2. TOP" << std::endl;
std::cout << "3. IsEmpty" << std::endl;
std::cout << "4. POP" << std::endl;
std::cout << "5. EXIT" << std::endl;
std::cout << "6. Print" << std::endl;
std::cout << "Enter the choice" << std::endl;
std::cin >> ch;
switch (ch)
{
case 1:
std::cout << "Enter the number to be pushed" << std::endl;
std::cin >> int_elem;
s1.push(int_elem);
break;
case 2:
std::cout << "Get the TOP Element" << std::endl;
try
{
s1.topElement();
}
catch (std::out_of_range &oor)
{
std::cerr << "Out of Range error:" << oor.what() << std::endl;
}
break;
case 3:
std::cout << "Check Empty" << std::endl;
s1.isEmpty();
break;
case 4:
std::cout << "POP the element" << std::endl;
try
{
s1.pop();
}
catch (const std::out_of_range &oor)
{
std::cerr << "Out of Range error: " << oor.what() << '\n';
}
break;
case 5:
exit(0);
case 6:
s1.print();
break;
default:
std::cout << "Enter a valid input";
break;
}
}
}
else if (choice == 2)
{
int ch = 1;
while (ch > 0)
{
std::cout << "\n1. PUSH" << std::endl;
std::cout << "2. TOP" << std::endl;
std::cout << "3. IsEmpty" << std::endl;
std::cout << "4. POP" << std::endl;
std::cout << "5. EXIT" << std::endl;
std::cout << "6. Print" << std::endl;
std::cout << "Enter the choice" << std::endl;
std::cin >> ch;
switch (ch)
{
case 1:
std::cout << "Enter the number to be pushed" << std::endl;
std::cin >> float_elem;
s2.push(float_elem);
break;
case 2:
std::cout << "Get the TOP Element" << std::endl;
try
{
s2.topElement();
}
catch (std::out_of_range &oor)
{
std::cerr << "Out of Range error:" << oor.what() << std::endl;
}
break;
case 3:
std::cout << "Check Empty" << std::endl;
s2.isEmpty();
break;
case 4:
std::cout << "POP the element" << std::endl;
try
{
s2.pop();
}
catch (const std::out_of_range &oor)
{
std::cerr << "Out of Range error: " << oor.what() << '\n';
}
break;
case 5:
exit(0);
case 6:
s2.print();
break;
default:
std::cout << "Enter a valid input";
break;
}
}
}
else if (choice == 3)
{
int ch = 1;
while (ch > 0)
{
std::cout << "\n1. PUSH" << std::endl;
std::cout << "2. TOP" << std::endl;
std::cout << "3. IsEmpty" << std::endl;
std::cout << "4. POP" << std::endl;
std::cout << "5. EXIT" << std::endl;
std::cout << "6. Print" << std::endl;
std::cout << "Enter the choice" << std::endl;
std::cin >> ch;
switch (ch)
{
case 1:
std::cout << "Enter the number to be pushed" << std::endl;
std::cin >> char_elem;
s3.push(char_elem);
break;
case 2:
std::cout << "Get the TOP Element" << std::endl;
try
{
s3.topElement();
}
catch (std::out_of_range &oor)
{
std::cerr << "Out of Range error:" << oor.what() << std::endl;
}
break;
case 3:
std::cout << "Check Empty" << std::endl;
s3.isEmpty();
break;
case 4:
std::cout << "POP the element" << std::endl;
try
{
s3.pop();
}
catch (const std::out_of_range &oor)
{
std::cerr << "Out of Range error: " << oor.what() << '\n';
}
break;
case 5:
exit(0);
case 6:
s3.print();
break;
default:
std::cout << "Enter a valid input";
break;
}
}
}
else
std::cout << "Invalid Choice";
std::cin.get();
}
实现 VECTOR STL 中存在的所有标准函数。这应该会增强您对矢量的理解。并且有问题的代码不是 VECTOR 的正确实现,因为 Vector STL 提供了许多其他功能。