What is the best way to create a new type of iterator?

T

tomerdr

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.
 
M

mlimber

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'?

You might consider using regular expressions, either from
std::tr1::regex (cf.
http://dinkumware.com/manuals/?manual=compleat&page=regex.html) or,
nearly equivalently, from Boost.Regex
(http://boost.org/libs/regex/doc/index.html). You might also consider
Boost.Tokenizer (http://boost.org/libs/tokenizer/index.html).

Cheers! --M
 
D

Daniel T.

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);
}
 
Y

Yahooooooooo

mlimber said:
You might consider using regular expressions, either from

Regular expressions works only when you know the format of input and
math exp are not specific format.

Regards,
M.Azmath
"Evergreen C++"
 
M

mlimber

Yahooooooooo said:
Regular expressions works only when you know the format of input and
math exp are not specific format.

But, you could still use them by breaking the expression up (e.g.,
"[0-9]+" would match any integral number, "[+-*/]" would match
operators, etc.).

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top