473,465 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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


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
5 1240
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
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

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
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
mlimber wrote:
[...]
"[+-*/]" would match operators
[...]

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

Cheers! --M

Dec 28 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
2
by: Alexander Stippler | last post by:
Hi, I've got a question concerning iterators. I'm writing some container class and iterators upon it. I have to have typedef typex pointer; typedef typey reference; to be standard conforming....
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
3
by: John Smith | last post by:
Hey I have some code which I've been using on Microsoft VC++ for some time. Now I wanted to port my application to Mac OS X which offers gcc and the build fails. Here is the troublesome code:...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
9
by: Jeff Dege | last post by:
I've been programming in C++ for a good long while, but there are aspects of the language I've never needed, and hence never bothered to really learn. It's the curse of working on a developed...
27
by: Steven D'Aprano | last post by:
I thought that an iterator was any object that follows the iterator protocol, that is, it has a next() method and an __iter__() method. But I'm having problems writing a class that acts as an...
8
by: watkinsdev | last post by:
Hi, I have created a mesh class in visual studio 6.0 c++. I can create a device, render objects and can edit the objects by for instancnce selecting a cluster of vertices and processing the...
2
by: Lambda | last post by:
The code is simple: // Token.h #ifndef TOKEN_H #define TOKEN_H #include <vector> #include <string> class Token
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.