push
This commit is contained in:
+6
-1
@@ -30,6 +30,7 @@ void Span::addNumber(int x)
|
||||
|
||||
if (this->_vec.size() >= this->_n)
|
||||
throw std::out_of_range("Span vector is full");
|
||||
|
||||
if (this->_vec.size() < this->_n)
|
||||
this->_vec.push_back(x);
|
||||
}
|
||||
@@ -38,6 +39,7 @@ int Span::longestSpan() const
|
||||
{
|
||||
if (_vec.empty() || _vec.size() == 1)
|
||||
throw std::out_of_range("Span vector has less than 2 values");
|
||||
|
||||
std::vector<int>::const_iterator maxIt = std::max_element(_vec.begin(), _vec.end());
|
||||
std::vector<int>::const_iterator minIt = std::min_element(_vec.begin(), _vec.end());
|
||||
return (*maxIt - *minIt);
|
||||
@@ -47,10 +49,13 @@ int Span::shortestSpan() const
|
||||
{
|
||||
if (_vec.empty() || _vec.size() == 1)
|
||||
throw std::out_of_range("Span vector has less than 2 values");
|
||||
|
||||
std::vector<int> tempvec = _vec;
|
||||
std::sort(tempvec.begin(), tempvec.end());
|
||||
int minspan = std::numeric_limits<int>::max();
|
||||
for (size_t i = 0; i < _vec.size() - 1; ++i) {
|
||||
|
||||
for (size_t i = 0; i < _vec.size() - 1; ++i)
|
||||
{
|
||||
int tempspan = tempvec[i + 1] - tempvec[i];
|
||||
if (tempspan < minspan)
|
||||
minspan = tempspan;
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user