Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- using namespace std;
- template <typename T>
- class SimpleVector {
- public:
- SimpleVector() = default;
- explicit SimpleVector(size_t size) : data(new T[size])
- , size(size)
- , capacity(size) {}
- ~SimpleVector() {
- delete[] data;
- }
- T& operator[](size_t index) {
- return data[index];
- }
- T* begin() {
- return data;
- }
- T* end() {
- return data + size;
- }
- [[nodiscard]] size_t Size() const {
- return size;
- }
- [[nodiscard]] size_t Capacity() const {
- return capacity;
- }
- void PushBack(const T& value) {
- if (size >= capacity) {
- auto new_cap = capacity == 0 ? 1 : 2 * capacity;
- auto new_data = new T[new_cap];
- copy(begin(), end(), new_data);
- delete[] data;
- data = new_data;
- capacity = new_cap;
- }
- data[size++] = value;
- }
- private:
- T* data = nullptr;
- size_t size = 0;
- size_t capacity = 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement