In article <11**********************@h40g2000cwb.googlegroups .com>,
to*****@gmail.com wrote:
Hi,
I have a string which contains a math expression:"100*33-55+2355"
And i need to create a postfix representation.
I would like to create an 'iterator' which give me the correct sub
expressions
it=expression.begin() ; //*it='100'
it++; //*it='*'
it++; //*it='33';
Since i am a beginner with stl,i am currently thinking to implement
the iterator design pattern
on the string.
Is there another way to do it using 'stl style'?
Thanks in advance.
Here is some sample code that creates an iterator that iterates over the
Fibonacci sequence. Hope it helps.
#include <iterator>
class fibonacci: public std::iterator< std::forward_iterator_tag, int >
{
int prev_value, value, max;
public:
fibonacci(): prev_value(0), value(0), max(0) { }
explicit fibonacci(int m): prev_value(0), value(1), max(m) { }
const int operator*() const { return value; }
fibonacci& operator++() {
int tmp = value;
value += prev_value;
prev_value = tmp;
return *this;
}
fibonacci operator++(int) {
fibonacci tmp(*this);
++(*this);
return tmp;
}
friend bool operator==(const fibonacci& lhs, const fibonacci& rhs) {
bool result = false;
if ( lhs.value == 0 && rhs.value == 0 )
result = true;
else if ( rhs.value == 0 && !( lhs.value < lhs.max ) )
result = true;
else if ( lhs.value == 0 && !( rhs.value < rhs.max ) )
result = true;
else if ( lhs.prev_value == rhs.prev_value &&
lhs.value == rhs.value && lhs.max == rhs.max )
result = true;
return result;
}
};
bool operator!=(const fibonacci& lhs, const fibonacci& rhs) {
return !(lhs == rhs);
}