Boost logo

Boost Users :

From: Rene Rivera (grafik.list_at_[hidden])
Date: 2005-05-29 14:37:32


Jeff Garland wrote:
> On Sun, 29 May 2005 00:21:33 -0400, Saurabh Sood wrote
>
>>Hi all,
>>
>> i am totally new to boost but more i am feeling good and bad at
>>same time ... good coz boost is cross-platform and powerful ..
>>however sometimes its hard to do simple things ...
>>
>> Take following for instance ..
>>
>
> -------------------------------------------------------------------------------------
>
>>using namespace boost::posix_time;
>>ptime p = = microsec_clock::local_time();
>>...
>>... (some calculations)
>>...
>>time_duration td = microsec_clock::local_time() - p;
>>std::cout << std::string(" Total Milliseconds: ")
>> << td.total_milliseconds()
>> << std::endl; // Works fine
>>
>
> -------------------------------------------------------------------------------------
>
>>But how can the value present in td.total_milliseconds() could be
>>put inside a std::string ? It might sound simple but i guess its
>>tricky . ...
>
>
> How about
> #include <sstream>
> //...
> std::stringstream ss;
> ss << td.total_milliseconds();
> std::string s = ss.str();
>
> After all, total_milliseconds just returns a numeric value.

Or possibly:

#include <boost/lexical_cast.hpp>
//...
std::string m =
   boost::lexical_cast<std::string>(td.total_milleseconds());

Amounts to the same code as Jeff's code. But depending on the context it
might be more convenient.

Docs: http://www.boost.org/libs/conversion/lexical_cast.htm

-- 
-- Grafik - Don't Assume Anything
-- Redshift Software, Inc. - http://redshift-software.com
-- rrivera/acm.org - grafik/redshift-software.com
-- 102708583/icq - grafikrobot/aim - Grafik/jabber.org

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net