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

Parsing A String In C++

P: 9
Hi,

Would anybody tell me an efficient method to parse a string inside a variable which is line separated ...

Like this

value1: line1 /n
value2: line2 /n
value3: line3 /n

i want to parse line y line first and then each of the values by id. Here i am talking about strings coming in at very high rate maybe be 1000s a second.

so it would be nice for you experts to gimme a effcient method for parsing a string inside a string variable(this is not a file).
Jun 12 '07 #1
Share this Question
Share on Google+
5 Replies


P: 79
If you're reading in from a file and the lines are separated by \n, you don't need to "parse". All you have to do is have:

while(!(input.eof())

And in that loop, you would have another loop and put the strings in an array. But this is simply reading in strings like you normally do:

input >> string;

I think that would work, but you should wait for a second opinion.
Jun 12 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
You might try a State machine. Check out the State Design Pattern in the book "Design Patterns" by Erich Fromm, et al. Addison-Wesley 1994.

There is also an article on this pattern in the C/C++ Articles forum.
Jun 12 '07 #3

P: 9
thanks ahamad for your replys. But i am not looking for parsing a string. What i have is a string variable. I must directly extract that string and do parsing operations on it. As i told you earlier, i am talking about 1000's of such strings per second. so putting into a file and reading it will not be feasible. If you have a better solution please let me know or is file operation feasible if the rate of the operation is 1000/ sec most of the times ?
Jun 13 '07 #4

P: 79
How would each line look like and what kind of parsing operations do you have in mind?

For example, someone may want to "parse" a string containing mathematical terms (numbers, equations etc) with their respective values:

pi = 3.1415926
squareArea = length^2
rectArea = length*width
...

So if someone wants to read in each line, and then split them into two parts (the term and its equation), you can do that by using tokenize or other methods. Is this the kind of thing you want to do?
Jun 13 '07 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
thanks ahamad for your replys. But i am not looking for parsing a string. What i have is a string variable. I must directly extract that string and do parsing operations on it. As i told you earlier, i am talking about 1000's of such strings per second. so putting into a file and reading it will not be feasible. If you have a better solution please let me know or is file operation feasible if the rate of the operation is 1000/ sec most of the times ?
Did you read the article on the State design pattern in the C/C++ Articles forum yet??

Part of the example is doing parsing operations on a string.
Jun 14 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.