Re: no number->string conversion in stdc++?

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

 



Shriramana Sharma wrote:

> I learnt today to my dismay that there is in fact no number to string 
> conversion function in the standard C++ library? I can't do simple 
> natural things like:
> 
> std :: string a ( 2 ) ;
> std :: string b ( 2.5 ) ;

These conversions are neither simple nor natural.

Look at the printf() documentation, and note how much of it applies to
formatting numeric values. Base, precision, field width, leading
zeroes/spaces, exponential notation. And that's aside from locale
issues (e.g. decimal separator), which aren't described there.

> The three ways out of this problem I got to know:
> 
> 1. stringstream from std, resulting in somewhat unweildy operations
> 2. lexical_cast from boost, resulting in dependency on huge boost lib
> 3. QString from Qt, resulting in depdency on huge Qt lib
> 
> Of course stdc++ is also a huge lib, but thought I can expect stdc++ to 
> be there on more machines than boost or Qt.
> 
> So shall I go with std::string and std::stringstream, std::string and 
> boost::lexical_cast or simply Qt's QString? QString seems most 
> appealing, but I would listen to your better judgment.

If the std::ostream formatting operators are sufficient, then use
std::ostringstream, otherwise write your own.

Don't use substantial external libraries just for a few utility
functions.

-- 
Glynn Clements <glynn@xxxxxxxxxxxxxxxxxx>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux