By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,127 Members | 939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,127 IT Pros & Developers. It's quick & easy.

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

P: n/a

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.

Dec 27 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
to*****@gmail.com wrote:
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/?manua...age=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

Dec 27 '06 #2

P: n/a
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);
}
Dec 28 '06 #3

P: n/a

mlimber wrote:
to*****@gmail.com wrote:
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
Regular expressions works only when you know the format of input and
math exp are not specific format.

Regards,
M.Azmath
"Evergreen C++"

Dec 28 '06 #4

P: n/a
Yahooooooooo wrote:
mlimber wrote:
to*****@gmail.com wrote:
I have a string which contains a math expression:"100*33-55+2355"
And i need to create a postfix representation.
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.
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

Dec 28 '06 #5

P: n/a
mlimber wrote:
[...]
"[+-*/]" would match operators
[...]

Of course that should be "[+\-*/]".

Cheers! --M

Dec 28 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.