You are on page 1of 19

Iterators

Input Iterator

Output Iterator

Forward Iterator

Bidirectional Iterator

Random Access Iterator

119

Abilities of Iterators
Iterator Category

Ability

Provider

Input iterator

Read forward

istream

Output iterator

Write forward

ostream, inserter

Forward iterator

Read and write


forward

Bidirectional iterator

Read and write


forward and backward

Random access
iterator

Read and write with


random access

list, set, multiset,


map, multimap,
vector, deque, string,
array

120

Input Iterator
Expression
*iter

Effect
Provides read access to the actual element

iter->member Provides read access to a member of the actual element


++iter

Steps forward (returns new position)

iter++

Steps forward (returns old position)

iter1 == iter2 Returns whether iter1 and iter2 are equal


iter1 != iter2

Returns whether iter1 and iter2 are not equal

TYPE(iter)

Copies iterator (copy contructor)

121

Output Iterator
Expression

Effect

*iter = value

Writes value to the actual element

++iter

Steps forward (returns new position)

iter++

Steps forward (returns old position)

TYPE(iter)

Copies iterator (copy contructor)

An output iterator is like a black hole.

122

Forward Iterator
Expression
*iter

Effect
Provides read access to the actual element

iter->member Provides read access to a member of the actual element


++iter

Steps forward (returns new position)

iter++

Steps forward (returns old position)

iter1 == iter2 Returns whether iter1 and iter2 are equal


iter1 != iter2

Returns whether iter1 and iter2 are not equal

TYPE()

Creates iterator (default constructor)

TYPE(iter)

Copies iterator (copy contructor)

iter1 = iter2

Assigns an iterator
123

Bidirectional Iterator
Expression
*iter

Effect
Provides read access to the actual element

iter->member Provides read access to a member of the actual element


++iter

Steps forward (returns new position)

iter++

Steps forward (returns old position)

--iter

Steps backward (returns new position)

iter--

Steps backward (returns old position)

iter1 == iter2 Returns whether iter1 and iter2 are equal


iter1 != iter2

Returns whether iter1 and iter2 are not equal

TYPE()

Creates iterator (default constructor)

TYPE(iter)

Copies iterator (copy contructor)

iter1 = iter2

Assigns an iterator
124

Random Access Iterator


Expression

Effect

iter[n]

Provides read access to element at index n

iter+= n

Steps n elements forward or backward

iter-= n

Steps n elements backward or forward

n+iter

Returns the iterator of the nth next element

iter-n

Returns the iterator of the nth previous element

iter1 - iter2

Returns distance between iter1 and iter2 are not equal

iter1 < iter2

Returns whether iter1 is before iter2

iter1 > iter2

Returns whether iter1 is after iter2

iter1 <= iter2

Returns whether iter1 is not after iter2

iter1 >= iter2

Returns whether iter1 is not before iter2


125

Class HuffmanBitCode
class HuffmanBitCode
{
private:
byte* fBitArray;
unsigned int fCodeLength;
void copy( const HuffmanBitCode& aCode );
void extend( int aToCodeLength );
void copy( byte aBitArray[], int aCodeLength );

};

126

Class HuffmanBitCode
class HuffmanBitCode
{

public:
HuffmanBitCode();
HuffmanBitCode( byte aBitArray[], int aCodeLength );
HuffmanBitCode( const HuffmanBitCode& aBitCode );
~HuffmanBitCode();
class iterarator { };
unsigned int size() const;
byte* getBits() const;
int lengthInByte() const;
void add0();
void add1();
HuffmanBitCode& operator=( const HuffmanBitCode& aBitCode ); // required bc copy constructor
int at( unsigned int aIndex ) const;
int operator[]( unsigned int aIndex ) const;
iterator begin();
iterator end();
};

127

Class HuffmanBitCode::iterator
// HuffmanBitCode::iterator
class iterator
{
friend class HuffmanBitCode;
private:
HuffmanBitCode* fCode;
unsigned int fIndex;
// follow singleton pattern (constructor should not be public)
protected:
iterator();
iterator( HuffmanBitCode* aCode );
iterator( HuffmanBitCode* aCode, unsigned int aPosition );

};

128

Class HuffmanBitCode::iterator
// HuffmanBitCode::iterator
class iterator
{

public:
// Iterator behavior
int operator*() const;
iterator& operator++(); // prefix
iterator operator++(int);// postix (extra unused argument)
bool operator==( const iterator& ) const;
bool operator!=( const iterator& ) const;
// non-standard iterator method to obtain a cloned end
iterator end();
iterator next();
};

129

HuffmanBitCode::iterator::operator++()
HuffmanBitCode::iterator&
HuffmanBitCode::iterator::operator++()
{
fIndex++;
return *this;
}

130

HuffmanBitCode::iterator::operator++(int)
HuffmanBitCode::iterator
HuffmanBitCode::iterator::operator++(int)
{
iterator lTemp = *this;
fIndex++;
return lTemp;
}

131

HuffmanBitCode::iterator::operator==()

bool HuffmanBitCode::iterator::operator==
( const iterator& aOther ) const
{
return (fCode == aOther.fCode) &&
(fIndex == aOther.fIndex);
}

132

HuffmanBitCode::iterator::operator!=()

bool HuffmanBitCode::iterator::operator!=
( const iterator& aOther ) const
{
return !(*this == aOther);
}

133

HuffmanBitCode::iterator::end()
HuffmanBitCode::iterator
HuffmanBitCode::iterator::end()
{
return
iterator( fCode, fCod->fCodeLength );
}

134

HuffmanBitCode::iterator::next()

HuffmanBitCode::iterator
HuffmanBitCode::iterator::next()
{
return iterator( fCode, fIndex + 1 );
}

135

HuffmanBitCode::begin()

HuffmanBitCode::iterator HuffmanBitCode::begin()
{
return iterator( this );
}

136

HuffmanBitCode::end()

HuffmanBitCode::iterator HuffmanBitCode::end()
{
return iterator( this, fCodeLength );
}

137

You might also like