Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template <typename T>
- class SimpleVector {
- public:
- SimpleVector() : arr_(new T[10]), size_(10) {}
- SimpleVector(int size) : arr_(new T[size]), size_(size) {}
- SimpleVector(const SimpleVector& object) : arr_(new T[object.size_] {}), size_(object.size_) {
- for (int i = 0; i < size_; i++) {
- arr_[i] = object.arr_[i];
- };
- }
- SimpleVector& operator=(const SimpleVector& object) {
- if (!(this == &object)) {
- delete[] arr_;
- arr_ = new T[object.size_];
- size_ = object.size_;
- for (int i = 0; i < size_; i++) {
- arr_[i] = object.arr_[i];
- }
- }
- return *this;
- }
- SimpleVector(SimpleVector&& object) : arr_(object.arr_), size_(object.size_) {
- object.arr_ = nullptr;
- object.size_ = 0;
- }
- SimpleVector& operator=(SimpleVector&& object) {
- if (!(this == &object)) {
- delete[] arr_;
- arr_ = object.arr_;
- size_ = object.size_;
- object.arr_ = nullptr;
- object.size_ = 0;
- }
- return *this;
- }
- T& operator[] (int index) {
- if (index >= 0 && index < size_) {
- return arr_[index];
- }
- }
- T operator[] (int index) const {
- if (index >= 0 && index < size_) {
- return arr_[index];
- }
- }
- int Size() const {
- return size_;
- }
- ~SimpleVector() {
- delete[] arr_;
- }
- private:
- T* arr_;
- int size_;
- };
- template <typename T>
- void sort(SimpleVector<T>& array) {
- for (int k = 0; k < array.Size(); k++) {
- for (int j = 0; j < array.Size() - 1; j++) {
- if (array[j] > array[j + 1]) {
- T temp = array[j];
- array[j] = array[j + 1];
- array[j + 1] = temp;
- }
- }
- }
- }
- template<typename T>
- void print(const SimpleVector<T>& array) {
- for (int i = 0; i < array.Size(); i++) {
- cout << array[i] << " ";
- }
- cout << endl;
- }
- int main() {
- SimpleVector<int> vec;
- for (int i = 0; i < vec.Size(); i++) {
- vec[i] = rand() % 10;
- }
- for (int i = 0; i < vec.Size(); i++) {
- cout << vec[i] << " ";
- }
- cout << endl;
- SimpleVector<int> int_vector;
- for (int i = 0; i < int_vector.Size(); i++) {
- int_vector[i] = rand() % 10;
- }
- cout << "int_vector:" << endl;
- print(int_vector);
- cout << endl;
- cout << "Sorted int_vector:" << endl;
- sort(int_vector);
- print(int_vector);
- cout << endl;
- SimpleVector<string> string_vector;
- string_vector[0] = "two";
- string_vector[1] = "seven";
- string_vector[2] = "zero";
- string_vector[3] = "four";
- string_vector[4] = "one";
- string_vector[5] = "three";
- string_vector[6] = "five";
- string_vector[7] = "six";
- string_vector[8] = "eight";
- string_vector[9] = "nine";
- cout << "string_vector:" << endl;
- print(string_vector);
- cout << endl;
- cout << "Sorted string_vector:" << endl;
- sort(string_vector);
- print(string_vector);
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment