Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <exception>
- template <class T>
- T& cool_min(T& a, T& b) {
- return (a < b) ? a : b;
- }
- class InvalidIndexException : public std::exception {
- public:
- explicit InvalidIndexException(int index) : index_(index) {}
- [[nodiscard]] int Index() const { return index_;}
- const char* what() {
- return "Invalid Index";
- }
- private:
- int index_;
- };
- template <int N, class T>
- class CArray {
- public:
- CArray() : arr_(new T[N]), size_(N) {
- }
- [[nodiscard]] int Size() const { return size_;}
- T& operator[] (const int index) {
- if(index < 0 || index >= size_)
- throw InvalidIndexException(index);
- return arr_[index];
- }
- private:
- T* arr_;
- int size_;
- };
- int main() {
- std::string s1 = "c", s2 = "b";
- cool_min(s1, s2);
- std::cout << cool_min(s1, s2) << "\n";
- CArray<5, int> arr;
- arr[4] = 3;
- std::cout << arr[4] << "\n";
- try {
- arr[8] = 123;
- }
- catch(InvalidIndexException &e) {
- std::cout << e.what() << "\n";
- std::cout << "Wrong index = " << e.Index() << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement