实现myVector
- #include
-
- using namespace std;
-
- template <typename T>
- class myVector {
- private:
- T* data;
- int size;
- int capacity;
- public:
- // 构造函数
- myVector() : data(nullptr), size(0), capacity(0) {}
- //拷贝构造函数
- myVector(const myVector& other):size(other.size), capacity(other.capacity){
- data = new T[other.capacity];
- for (int i = 0; i < size; i++) {
- data[i] = other.data[i];
- }
- }
- // 拷贝赋值函数
- myVector& operator=(const myVector& other) {
- if (this != &other) {
- // 释放旧内存
- delete[] data;
-
- // 复制大小和容量
- this->size = other.size;
- this->capacity = other.capacity;
-
- // 为新对象分配内存
- data = new T[capacity];
-
- // 复制元素到新对象
- for (int i = 0; i < size; i++) {
- data[i] = other.data[i];
- }
- }
- return *this;
- }
- // 析构函数
- ~myVector() {
- delete[] data;
- }
- // 末尾添加元素
- void push_back(const T& element) {
- if (size == capacity) { // 容量不足时,分配更多空间
- if (capacity == 0) {
- capacity = 1;
- } else {
- capacity *= 2;
- }
- //重新创建一个空间
- T* newData = new T[capacity];
- // 复制元素到新空间
- for (int i = 0; i < size; ++i) {
- newData[i] = data[i];
- }
- delete[] data;
- data = newData;
- }
- data[size++] = element;
- }
- //末尾删除元素
- void pop_back(){
- if(empty())
- {
- throw string("is empty");
- }
- size--;
- }
- //判断Vector是否为空
- bool empty(){
- return size==0;
- }
- // 获取元素数量
- int getSize_() const {
- return size;
- }
- // 获取容器容量
- int getCapacity_() const {
- return capacity;
- }
- // 访问元素
- T& operator[](int index) {
- if (index < size) {
- return data[index];
- } else {
- throw string("Index out of range");
- }
- }
- // 迭代器
- class iterator {
- private:
- T* ptr;
- public:
- //构造函数
- iterator(T* p) : ptr(p) {}
- //*运算符重载
- T& operator*() {
- return *ptr;
- }
- //++it运算符重载
- iterator& operator++() {
- ++ptr;
- return *this;
- }
- //it++运算符重载
- iterator operator++(int) {
- iterator temp = *this;
- ++ptr;
- return temp;
- }
- //!=运算符重载
- bool operator!=(const iterator& other) const {
- return ptr != other.ptr;
- }
- };
-
- iterator begin() {
- return iterator(data);
- }
-
- iterator end() {
- return iterator(data + size);
- }
- };
-
- int main() {
- myVector<int> v1; //无参构造
-
- for (int i=0; i<10; i++) { //添加元素
- v1.push_back(i);
- }
-
- myVector<int>::iterator it1 = v1.begin(); //v1迭代器
- cout<<"v1>>";
- for (; it1 != v1.end(); it1++) { //show元素
- cout << *it1 << " ";
- }
- cout<
-
- myVector<int> v2 = v1; //拷贝构造
- myVector<int>::iterator it2 = v2.begin(); //v2迭代器
- cout<<"v2>>";
- for (; it2 != v2.end(); it2++) { //show元素
- cout << *it2 << " ";
- }
- cout<
-
- myVector<int> v3; //无参构造
- v3 = v1; //拷贝赋值
- cout<<"is empty? = "<
empty()< - v3.pop_back(); // 队尾删除元素
- cout<<"size = "<
getSize_()< - cout<<"capacity = "<
getCapacity_()< - myVector<int>::iterator it3 = v3.begin(); //v3迭代器
- cout<<"v3>>";
- for (; it3 != v3.end(); it3++) { //show元素
- cout << *it3 << " ";
- }
-
-
相关阅读:
从零学习 InfiniBand-network架构(六)—IB协议链路层QoS如何实现
「Qt中文教程指南」如何创建基于Qt Widget的应用程序(一)
世界杯中隐藏的IoT物联网黑科技
供应商关系管理的重要性
【老生谈算法】matlab实现LCMV和LCEC算法抗干扰性能比较研究——LCEC算法
【EI会议】第三届信息控制、电气工程及轨道交通国际学术会议(ICEERT 2023)
0531作业 链表
如何快速为团队打造自己的组件库(下)—— 基于 element-ui 为团队打造自己的组件库
ChatGPT怎么运用在文学分析和文化研究中?
教你注册chrome开发者账号,并发布chrome浏览器插件。
-
原文地址:https://blog.csdn.net/mcslll/article/details/132889426