CPP08 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
Span.hpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* Span.hpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/08/21 23:41:39 by kamitsui #+# #+# */
9/* Updated: 2025/08/21 23:50:24 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
18#ifndef SPAN_HPP
19#define SPAN_HPP
20
21#include <algorithm>
22#include <iostream>
23#include <iterator> // For std::distance
24#include <stdexcept>
25#include <vector>
26
32class Span {
33 private:
34 unsigned int _maxSize;
35 std::vector<int> _numbers;
36
37 // The default constructor is private to prevent instantiation without specifying the size.
38 Span();
39
40 public:
45 explicit Span(unsigned int N);
46
51 Span(const Span &other);
52
58 Span &operator=(Span other);
59
63 ~Span();
64
69 void swap(Span &other);
70
76 void addNumber(int number);
77
85 template <typename InputIterator> void addNumber(InputIterator first, InputIterator last) {
86 // Check if there is enough space
87 if (static_cast<unsigned long>(std::distance(first, last)) + _numbers.size() > _maxSize) {
89 }
90 // Insert the elements from the iterator range
91 _numbers.insert(_numbers.end(), first, last);
92 }
93
99 int shortestSpan();
100
106 int longestSpan();
107
113 public:
114 virtual const char *what() const throw() { return "Span is full, cannot add more numbers."; }
115 };
116
122 public:
123 virtual const char *what() const throw() { return "Not enough numbers in Span to find a span."; }
124 };
125};
126
127#endif
128
129// Before Refactor
130//{
131// // --- Assignment operator ---
132// Span &operator=(const Span &other);
133//
134// // --- Member function to add a range of numbers ---
135// template <typename InputIterator> void addNumber(InputIterator first, InputIterator last) {
136// // Check if there is enough space
137// if (std::distance(first, last) + _numbers.size() > _maxSize) {
138// throw Span::SpanFullException();
139// }
140// // Insert the elements from the iterator range
141// _numbers.insert(_numbers.end(), first, last);
142// }
143//}
An exception thrown when there are insufficient numbers to calculate the span.
Definition Span.hpp:121
virtual const char * what() const
Definition Span.hpp:123
Exception thrown when Span is full.
Definition Span.hpp:112
virtual const char * what() const
Definition Span.hpp:114
A class that stores up to N integers and calculates the shortest and longest spans.
Definition Span.hpp:32
void addNumber(InputIterator first, InputIterator last)
Adds multiple numbers specifying the iterator range.
Definition Span.hpp:85
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 distance(T... args)
T end(T... args)
T insert(T... args)
T size(T... args)