-
April 18th, 2009, 10:14 PM
#1
int to string
Just wondered if I have missed something and if the following code could be more elegant?
I'm hunting through a network trace. Each row is in s_current_row. If I find one of the searchstrings (could be multiple occurrences of each searchstring) stored in array searchstring[x] then I replace it with some values including the integer that is the index of the searchstring.
Any thoughts for still a newbie very welcome, Nigel
Code:
// loop through search strings
int result;
for (int x = 1; x <= countdef; x++)
{
while ((result = s_current_row.find(searchstring[x], 0)) != -1)
{
string temp = "***";
stringstream out;
out << x;
temp += out.str();
temp += "|";
temp += database[x];
temp += "|";
temp += field[x];
temp += "***";
s_current_row.replace( result, searchstring[x].size(), temp );
}
}
Last edited by nigelhoath; April 18th, 2009 at 10:17 PM.
-
April 19th, 2009, 12:35 AM
#2
Re: int to string
You could use boost::lexical_cast to save a few lines
Code:
string temp("***" + boost::lexical_cast<string>(x) + "|");
temp += database[x];
// etc
But unless you're needing to do quite a few conversions or already have a dependecy on boost, I don't think I'd bother.
On a side note, in your while statement you should really compare the find result to string::npos instead of -1.
-
April 19th, 2009, 04:20 AM
#3
Re: int to string
speedo: I haven't checked out boost yet and as this stuff is compiled on lots of different environs I want to stay as clean (traditional - standard) as I can.
Yes - I can see -1 is a potential pitfall.
Gracias Amigo
-
April 19th, 2009, 07:22 AM
#4
Re: int to string
Incidentally, it might be better to do this instead:
Code:
stringstream out;
out << "***" << x << "|" << database[x] << "|" << field[x] << "***";
s_current_row.replace( result, searchstring[x].size(), out.str() );
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|