Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <array>
- #include <stdexcept>
- #include <iterator>
- #include <string>
- using namespace std;
- template <typename T, size_t N>
- class StackVector {
- public:
- explicit StackVector(size_t a_size = 0) : index_(a_size) {
- if (a_size > arr.size()) {
- throw invalid_argument("Expectations far too large");
- }
- }
- T& operator[](size_t index) {
- if (index >= index_) {
- throw invalid_argument("Out of bounds");
- }
- return arr[index];
- }
- const T& operator[](size_t index) const {
- try {
- return arr[index];
- } catch (invalid_argument& e) {
- throw(e.what());
- }
- }
- typename array<T, N>::iterator begin() {
- return arr.begin();
- }
- typename array<T, N>::iterator end() {
- return arr.begin() + index_;
- }
- typename array<T, N>::iterator begin() const {
- return begin();
- }
- typename array<T, N>::iterator end() const {
- return end();
- }
- size_t Size() const {
- return index_;
- }
- size_t Capacity() const {
- return arr.size();
- }
- void PushBack(const T& value) {
- if(index_ >= arr.size()) {
- throw overflow_error("Container full");
- }
- arr[index_++] = value;
- }
- T PopBack() {
- if(index_ == 0) {
- throw underflow_error("Container empty");
- }
- return arr[--index_];
- }
- private:
- size_t index_ = 0;
- array<T, N> arr;
- };
Advertisement
Add Comment
Please, Sign In to add comment