CPP08 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
Span.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* Span.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/08/22 00:09:15 by kamitsui #+# #+# */
9/* Updated: 2025/08/22 00:14:45 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
17#include "Span.hpp"
18#include <algorithm> // for std::swap
19#include <limits> // for std::numeric_limits
20
21// Constructor
22Span::Span(unsigned int N) : _maxSize(N) {
23 _numbers.reserve(N); // Reserve capacity in the vector to avoid reallocations if possible
24}
25
26// Copy Constructor
27Span::Span(const Span &other) : _maxSize(other._maxSize), _numbers(other._numbers) {}
28
29// swap member function
30void Span::swap(Span &other) {
31 std::swap(_maxSize, other._maxSize);
32 _numbers.swap(other._numbers);
33}
34
35// Assignment Operator (Apply the copy-and-swap idiom)
37 this->swap(other);
38 return *this;
39}
40
41// Destructor
43
44// Add a single number to the Span
45void Span::addNumber(int number) {
46 if (_numbers.size() >= _maxSize) {
48 }
49 _numbers.push_back(number);
50}
51
52// Find the shortest span between numbers
54 if (_numbers.size() <= 1) {
56 }
57
58 // Create a sorted copy of the vector to find differences between adjacent elements
59 std::vector<int> sorted = _numbers;
60 std::sort(sorted.begin(), sorted.end());
61
62 long minSpan = std::numeric_limits<long>::max();
63
64 for (size_t i = 1; i < sorted.size(); ++i) {
65 long currentSpan = static_cast<long>(sorted[i]) - static_cast<long>(sorted[i - 1]);
66 if (currentSpan < minSpan) {
67 minSpan = currentSpan;
68 }
69 }
70 return static_cast<int>(minSpan);
71}
72
73// Find the longest span between numbers
75 if (_numbers.size() <= 1) {
77 }
78
79 // The longest span is simply the difference between the max and min elements
80 int minVal = *std::min_element(_numbers.begin(), _numbers.end());
81 int maxVal = *std::max_element(_numbers.begin(), _numbers.end());
82
83 return maxVal - minVal;
84}
85
86// Before Refactor
87// --- Assignment Operator ---
88// Span &Span::operator=(const Span &other) {
89// if (this != &other) {
90// _maxSize = other._maxSize;
91// _numbers = other._numbers;
92// }
93// return *this;
94//}
95//
96// --- Copy Constructor ---
97// Span::Span(const Span &other) { *this = other; }
Provides a class that stores N integers and calculates their shortest and longest spans.
T begin(T... args)
An exception thrown when there are insufficient numbers to calculate the span.
Definition Span.hpp:121
Exception thrown when Span is full.
Definition Span.hpp:112
A class that stores up to N integers and calculates the shortest and longest spans.
Definition Span.hpp:32
void swap(Span &other)
Swaps the contents of two Span objects.
Definition Span.cpp:30
int shortestSpan()
Calculates the shortest span between stored numbers.
Definition Span.cpp:53
~Span()
Destructor.
Definition Span.cpp:42
int longestSpan()
Calculates the longest span between stored numbers.
Definition Span.cpp:74
Span & operator=(Span other)
Assignment operator (copy-and-swap idiom).
Definition Span.cpp:36
void addNumber(int number)
Adds a single number to the span.
Definition Span.cpp:45
T end(T... args)
T max_element(T... args)
T max(T... args)
T min_element(T... args)
T push_back(T... args)
T reserve(T... args)
T size(T... args)
T sort(T... args)
T swap(T... args)