Mike Chirico on 19 Nov 2003 09:31:02 -0500


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[PLUG] (no subject)


Sorry about the newbie question. What's the best way 
to convert numbers to strings in C++. The following works,
but is it better than using the sprintf() "old C" way of 
converting?


#include <iostream>
#include <string>
#include <sstream>
#include <list>

using namespace std;
int main(void)
{
  list<string> ml;
  string s;
  stringstream ss;



  for(int i=0; i < 3000; ++i)
    {
       ss << "sample text " << i ;
       ml.push_front( ss.str() );
       ss.str("");
    }

  cout << ml.front() << endl;
}


What about the fact that the stringstream has to be cleared
ss.str("")...does that incur additional overhead? Maybe 
there is a better way?

Anyway..still reading through "The C++ Programming Lang", 
Stroustrup and The C++ Standard Lib, Josuttis for an answer.

Regards,


Mike Chirico
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug