写一个在vs2019上能运行的#include <iostream>#include <stdlib.h>using namespace std;template <typename T>class Vector{public: Vector() : m_size(0), m_capacity(0), m_data(nullptr) {} Vector(int n, const T& val) : m_size(0), m_capacity(0), m_data(nullptr) { assign(n, val); } Vector(const Vector& other) : m_size(0), m_capacity(0), m_data(nullptr) { assign(other); } Vector& operator=(const Vector& other); T& operator[](int i) { return m_data[i]; } const T& operator[](int i) const { return m_data[i]; } void push_back(const T& val); void insert(int pos, const T& val); void clear(); int size() const { return m_size; } bool empty() const { return m_size == 0; } void erase(int pos);private: void assign(int n, const T& val); void assign(const Vector& other); void reserve(int n); void resize(int n); void destroy();private: int m_size; int m_capacity; T* m_data;};template <typename T>Vector<T>& Vector<T>::operator=(const Vector<T>& other){ if (this != &other) { destroy(); assign(other); } return *this;}template <typename T>void Vector<T>::push_back(const T& val){ if (m_size == m_capacity) { reserve(max(2 * m_capacity, 1)); } m_data[m_size++] = val;}template <typename T>void Vector<T>::insert(int pos, const T& val){ if (pos < 0 || pos > m_size) { return; } if (m_size == m_capacity) { reserve(max(2 * m_capacity, 1)); } for (int i = m_size - 1; i >= pos; i--) { m_data[i + 1] = m_data[i]; } m_data[pos] = val; m_size++;}template <typename T>void Vector<T>::clear(){ destroy(); m_size = 0;}template <typename T>void Vector<T>::erase(int pos){ if (pos < 0 || pos >= m_size) { return; } for (int i = pos; i < m_size - 1; i++) { m_data[i] = m_data[i + 1]; } m_size--;}template <typename T>void Vector<T>::assign(int n, const T& val){ resize(n); for (int i = 0; i < m_size; i++) { m_data[i] = val; }}template <typename T>void Vector<T>::assign(const Vector<T>& other){ resize(other.m_size); for (int i = 0; i < m_size; i++) { m_data[i] = other.m_data[i]; }}template <typename T>void Vector<T>::reserve(int n){ if (n <= m_capacity) { return; } T* new_data = new T[n]; for (int i = 0; i < m_size; i++) { new_data[i] = m_data[i]; } delete[] m_data; m_data = new_data; m_capacity = n;}template <typename T>void Vector<T>::resize(int n){ reserve(n); if (n >= m_size) { for (int i = m_size; i < n; i++) { m_data[i] = T(); } } m_size = n;}template <typename T>void Vector<T>::destroy(){ if (m_data != nullptr) { delete[] m_data; m_data = nullptr; m_capacity = 0; }}int main(){ Vector<int> vec; cout << "push_back 1, 2, 3" << endl; vec.push_back(1); vec.push_back(2); vec.push_back(3); cout << "size: " << vec.size() << endl; cout << "empty: " << vec.empty() << endl; cout << "insert 0 at pos 0" << endl; vec.insert(0, 0); cout << "size: " << vec.size() << endl; cout << "erase at pos 1" << endl; vec.erase(1); cout << "size: " << vec.size() << endl; cout << "clear" << endl; vec.clear(); cout << "size: " << vec.size() << endl; cout << "empty: " << vec.empty() << endl; return 0;}
时间: 2023-06-09 10:08:53 浏览: 95
程序需要以下步骤:
1. 打开Visual Studio 2019软件。
2. 点击"新建项目",选择"C++控制台应用程序"。
3. 输入项目名称和文件保存路径,然后点击"确定"。
4. 选择"C++17"作为项目的编译标准。
5. 在项目源文件中编写代码。比如输出"Hello, World!"可以使用以下代码:
#include <iostream>
int main()
{
std::cout << "Hello, World!\n";
return 0;
}
6. 点击"运行"按钮或者按下"F5"键编译并运行程序。
7. 程序运行成功后,在输出窗口中会打印出"Hello, World!"的字符串。
阅读全文