vector的简易实现

  vector的简易实现整理自《数据结构与算法分析–C++描述(第3版)》3.4节“向量的实现”。详细可参考《STL源码分析》4.2节。

  具体实现代码如下:

  1 #ifndef VECTOR_H
  2 #define VECTOR_H
  3 
  4 #include <iostream>
  5 using namespace std;
  6 
  7 template<class T>
  8 class Vector
  9 {
 10 private:
 11     int theSize;
 12     int theCapacity;
 13     T *objects;
 14 
 15 public:
 16     typedef T* iterator;
 17     typedef const T* const_iterator;
 18     enum{ SPARE_CAPACITY = 16 };
 19 
 20 public:
 21     explicit Vector(int initSize = 0);    //用explicit避免隐式类型转换
 22     ~Vector();
 23     Vector(const Vector<T>& rhs);
 24     const Vector<T>& operator=(const Vector<T>& rhs);
 25 
 26     void resize(int newSize);
 27     void reserve(int newCapacity);
 28 
 29     T& operator[](int index);
 30     const T& operator[](int index) const;
 31 
 32     bool empty() const;
 33     int size() const;
 34     int capacity() const;
 35 
 36     void push_back(const T& x);
 37     void pop_back();
 38     const T& back() const;
 39 
 40     iterator begin();
 41     const_iterator begin() const;
 42 
 43     iterator end();
 44     const_iterator end() const;
 45 
 46 };
 47 
 48 template<class T>
 49 Vector<T>::Vector(int initSize = 0) : theSize(initSize), theCapacity(initSize + SPARE_CAPACITY)
 50 {
 51     objects = new T[theCapacity];
 52 }
 53 
 54 template<class T>
 55 Vector<T>::~Vector()
 56 {
 57     delete []objects;
 58 }
 59 
 60 template<class T>
 61 Vector<T>::Vector(const Vector<T>& rhs) : objects(nullptr)
 62 {
 63     operator = (rhs);
 64 }
 65 
 66 template<class T>
 67 const Vector<T>& Vector<T>::operator=(const Vector<T>& rhs)
 68 {
 69     if (this != &rhs)
 70     {
 71         delete[]objects;
 72         theSize = rhs.size();
 73         theCapcity = rhs.theCapacity;
 74 
 75         // 注意是深拷贝
 76         objects = new T[capacity()];
 77         for (int k = 0; k < size(); k++)
 78             objects[k] = rhs.objects[k];
 79     }
 80 
 81     return *this;
 82 }
 83 
 84 template<class T>
 85 void Vector<T>::resize(int newSize)
 86 {
 87     if (newSize > theCapacity)
 88         reserve(newSize * 2 + 1);    //每次空间不够的时候,就重新获得2倍于当前容量的空间,+1是为了防止0的情况
 89     theSize = newSize;
 90 }
 91 
 92 template<class T>
 93 void Vector<T>::reserve(int newCapacity)
 94 {
 95     if (newCapacity < theSize)
 96         return;
 97 
 98     T *oldArray = objects;
 99 
100     // 之所以需要将老的数组复制到新的数组,是因为要保证Vector整块内存的连续性
101     objects = new T[newCapacity];
102     for (int k = 0; k < theSize; k++)
103         objects[k] = oldArray[k];
104 
105     theCapacity = newCapacity;
106 
107     delete []oldArray;
108 }
109 
110 template<class T>
111 T& Vector<T>::operator[](int index)
112 {
113     return objects[index];
114 }
115 
116 template<class T>
117 const T& Vector<T>::operator[](int index) const
118 {
119     return objects[index];
120 }
121 
122 template<class T>
123 bool Vector<T>::empty() const
124 {
125     return size() == 0;
126 }
127 
128 template<class T>
129 int Vector<T>::size() const
130 {
131     return theSize;
132 }
133 
134 template<class T>
135 int Vector<T>::capacity() const
136 {
137     return theCapacity;
138 }
139 
140 template<class T>
141 void Vector<T>::push_back(const T& x)
142 {
143     if (theSize == theCapacity)
144         reserve(theCapacity * 2 + 1);
145     objects[theSize++] = x;
146 }
147 
148 template<class T>
149 void Vector<T>::pop_back()
150 {
151     theSize--;
152 }
153 
154 template<class T>
155 const T& Vector<T>::back() const
156 {
157     return objects[theSize - 1];
158 }
159 
160 template<class T>
161 T* Vector<T>::begin()
162 {
163     return &objects[0];
164 }
165 
166 template<class T>
167 const T* Vector<T>::begin() const
168 {
169     return &objects[0];
170 }
171 
172 template<class T>
173 T* Vector<T>::end()
174 {
175     return &objects[size() - 1];
176 }
177 
178 template<class T>
179 const T* Vector<T>::end() const
180 {
181     return &objects[size() - 1];
182 }
183 
184 #endif

 

   

转载于:https://www.cnblogs.com/xiehongfeng100/p/4706193.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
词法分析器是编译器的第一个阶段,用于将源代码分解成单个的词语(Token)。以下是一个简单C++实现: ```cpp #include <iostream> #include <string> #include <vector> using namespace std; // 定义Token类 class Token { public: string type; // 类型 string value; // 值 Token(string type, string value) { this->type = type; this->value = value; } }; // 定义词法分析器类 class Lexer { private: string input; // 输入代码 int position; // 当前位置 char current; // 当前字符 public: Lexer(string input) { this->input = input; position = 0; current = input[position]; } // 获取下一个字符 void advance() { position++; if (position < input.size()) { current = input[position]; } else { current = '\0'; } } // 获取数字 Token get_number() { string number = ""; while (current >= '0' && current <= '9') { number += current; advance(); } return Token("NUMBER", number); } // 获取符号 Token get_symbol() { string symbol = ""; while (current == '+' || current == '-') { symbol += current; advance(); } return Token("SYMBOL", symbol); } // 分析输入代码 vector<Token> analyze() { vector<Token> tokens; while (current != '\0') { if (current >= '0' && current <= '9') { tokens.push_back(get_number()); } else if (current == '+' || current == '-') { tokens.push_back(get_symbol()); } else { advance(); } } return tokens; } }; int main() { string input = "12+34-56"; Lexer lexer(input); vector<Token> tokens = lexer.analyze(); for (int i = 0; i < tokens.size(); i++) { cout << "Token: " << tokens[i].type << ", Value: " << tokens[i].value << endl; } return 0; } ``` 以上代码实现了一个简单的词法分析器,可以将输入的代码分解成数字和符号两种类型的Token,并输出它们的类型和值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值