int to string
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: int to string

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    48

    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 11:17 PM.

  2. #2
    Join Date
    Aug 2007
    Posts
    858

    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.

  3. #3
    Join Date
    Mar 2009
    Posts
    48

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,357

    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() );
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center