User Tools

Site Tools


in204:tds:sujets:td4:part2

This is an old revision of the document!


Partie II – Création d’itérateurs

TD4

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 mPosition; // -1 if denotes no more elements.
 
	interval_iterator(const interval& anInterval, int aPosition) : 
		mInterval(&anInterval), mPosition(aPosition) {}
 
public:
	interval_iterator(const interval_iterator& anotherIterator):
		mInterval(anotherIterator.mInterval),
		mPosition(anotherIterator.mPosition) {}
 
	interval_iterator& operator = (interval_iterator& anotherIterator)
	{
		std::swap(mInterval, anotherIterator.mInterval);
		std::swap(mPosition, anotherIterator.mPosition);
		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;
 
};

Question 1.0

Complétez les classes interval et interval_iterator afin de pouvoir supporter les itérateurs.

Question 2.0

Tester le bon fonctionnement de classe avec le code suivant :

in204/tds/sujets/td4/part2.1571131902.txt.gz · Last modified: 2019/10/15 09:31 by bmonsuez