This is an old revision of the document!
Nous considérons un ensemble de nombres entiers correspondant à un interval fermé entre [minValue
, maxValue
] défini par la classe suivante :
class interval { private: int minValue; int maxValue; public: typedef int value_type; typedef size_t size_type; typedef ptrdiff_t difference_type; interval(int theMinValue, int theMaxValue) : minValue(theMinValue), maxValue(theMaxValue) {} interval(const interval& anotherInterval): minValue(anotherInterval.minValue), maxValue(anotherInterval.maxValue) {} interval& operator = (const interval& anotherInterval) { minValue = anotherInterval.minValue; maxValue = anotherInterval.maxValue; return *this; } size_type size() const { return (size_type)(maxValue - minValue); } int operator[](size_type anIndex) const { if (anIndex > size()) throw std::out_of_range("Index out of range"); return minValue + (int)anIndex; } bool operator == (const interval& anotherInterval) const { return anotherInterval.maxValue == maxValue && anotherInterval.minValue == minValue; } bool operator != (const interval& anotherInterval) const { return anotherInterval.maxValue != maxValue || anotherInterval.minValue != minValue; } };
Nous souhaitons pouvoir définir des itérateurs pour pouvoir itérer sur cet interval. Pour ce faire, nous devons ajouter au moins les méthodes suivantes et champs suivants :
friend class interval_iterator; typedef interval_iterator const_iterator; const_iterator begin() const noexcept; const_iterator end() const noexcept;
En supposant que la classe itérateur s’appelle interval_iterator.
Il ne reste plus qu’à créer une classe interval_iterator
dont le squelette serait le suivant :
class interval_iterator : public std::iterator <std::forward_iterator_tag, int> { private: friend class interval; const interval* mInterval; // Référence à l'interval. int mCurrent; // la valeur courante dont on fait référence. interval_iterator(const interval& anInterval, int aCurrentValue) : mInterval(&anInterval), mCurrent(aCurrentValue) {} public: interval_iterator(const interval_iterator& anotherIterator): mInterval(anotherIterator.mInterval), mCurrent(anotherIterator.mCurrent) {} interval_iterator& operator = (interval_iterator& anotherIterator) { mInterval = anotherIterator.mInterval; mCurrent = anotherIterator.mCurrent; return *this; } const reference operator*() const; const pointer operator->() const; interval_iterator& operator++(); interval_iterator& operator++(int); bool operator ==(const interval_iterator&) const; bool operator !=(const interval_iterator&) const; };
Complétez les classes interval
et interval_iterator
afin de pouvoir supporter les itérateurs.
Tester le bon fonctionnement de classe avec le code suivant :
template<class inputIterator> void print(inputIterator beginIt, inputIterator endIt) { typedef inputIterator::value_type value_type; value_type current_value, previous_value; if(beginIt == endIt) { std::cout << "[]" << std::endl; return; } for(;;) { std::cout << *beginIt; beginIt++; if(beginIt == endIt) break; std::cout << ", "; } std::cout << "]" << std::endl; return; } void testInterval() { interval inter(5, 15); std::cout << "Contenu de l'interval : "; print(inter.begin(), inter.end()); // Duplication des valeurs dans un vecteur std::vector<int> vec(inter.begin(), inter.end()); std::cout << "Contenu du vecteur : "; print(vec.begin(), vec.end()); } int main() { testInterval(); return 0; }