diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/cpp/int_container | |
add: graphs et rushs
Diffstat (limited to 'graphs/cpp/int_container')
| -rw-r--r-- | graphs/cpp/int_container/int_container.cc | 83 | ||||
| -rw-r--r-- | graphs/cpp/int_container/int_container.hh | 45 |
2 files changed, 128 insertions, 0 deletions
diff --git a/graphs/cpp/int_container/int_container.cc b/graphs/cpp/int_container/int_container.cc new file mode 100644 index 0000000..aabb5ab --- /dev/null +++ b/graphs/cpp/int_container/int_container.cc @@ -0,0 +1,83 @@ +#include "int_container.hh" +MyIntContainer::MyIntContainer(size_t size) + : current_size_{ 0 } + , max_size_{ size } + , elems_{ std::make_unique<int[]>(size) } +{} +void MyIntContainer::print() const +{ + if (current_size_ == 0) + return; + std::cout << elems_[0]; + for (size_t i = 1; i < current_size_; ++i) + { + std::cout << " | " << elems_[i]; + } + std::cout << "\n"; +} +size_t MyIntContainer::get_len() const +{ + return current_size_; +} +bool MyIntContainer::add(int elem) +{ + if (current_size_ == max_size_) + return false; + elems_[current_size_++] = elem; + return true; +} +std::optional<int> MyIntContainer::pop() +{ + if (current_size_ == 0) + return std::nullopt; + int res = elems_[--current_size_]; + elems_[current_size_] = -1; + return res; +} +std::optional<int> MyIntContainer::get(size_t position) const +{ + if (position >= current_size_) + return std::nullopt; + return elems_[position]; +} +std::optional<size_t> MyIntContainer::find(int elem) const +{ + size_t i; + for (i = 0; i < current_size_ && elems_[i] != elem; ++i) + continue; + if (i == current_size_) + return std::nullopt; + return i; +} +void swap(int& a, int& b) +{ + int tmp = a; + a = b; + b = tmp; +} +void MyIntContainer::sort() +{ + for (size_t i = 0; i < current_size_ - 1; i++) + { + bool flag = false; + for (size_t j = 0; j < current_size_ - i - 1; j++) + { + if (elems_[j] > elems_[j + 1]) + { + swap(elems_[j], elems_[j + 1]); + flag = true; + } + } + if (!flag) + break; + } +} +bool MyIntContainer::is_sorted() const +{ + for (size_t i = 1; i < current_size_; i++) + { + if (elems_[i - 1] > elems_[i]) + return false; + } + return true; +}
\ No newline at end of file diff --git a/graphs/cpp/int_container/int_container.hh b/graphs/cpp/int_container/int_container.hh new file mode 100644 index 0000000..2df772a --- /dev/null +++ b/graphs/cpp/int_container/int_container.hh @@ -0,0 +1,45 @@ +#pragma once + +#include <iostream> +#include <memory> +#include <optional> + +class MyIntContainer +{ +public: + MyIntContainer(size_t size); + + // Print the content of the container + void print() const; + + // Get the current number of elements inside the array + size_t get_len() const; + + // Add an element inside the array + bool add(int elem); + + // Get the last element inside the array and remove it + std::optional<int> pop(); + + // Get the element at a given position + std::optional<int> get(size_t position) const; + + // Get the index inside the array of a given element + std::optional<size_t> find(int elem) const; + + // Sort the array + void sort(); + + // Checks if the array is sorted + bool is_sorted() const; + +private: + // Current size of the elems_ array + size_t current_size_; + + // Maximum size of the elems_ array + size_t max_size_; + + // Array containing the elements + std::unique_ptr<int[]> elems_; +}; |
