Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Probably the most common easy way wraps essentially your second choice into a template named <code>lexical_cast</code>, such as the one in <a href="http://www.boost.org/doc/libs/release/doc/html/boost_lexical_cast.html" rel="noreferrer">Boost</a>, so your code looks like this:</p> <pre><code>int a = 10; string s = lexical_cast&lt;string&gt;(a); </code></pre> <p>One nicety of this is that it supports other casts as well (e.g., in the opposite direction works just as well).</p> <p>Also note that although Boost lexical_cast started out as just writing to a stringstream, then extracting back out of the stream, it now has a couple of additions. First of all, specializations for quite a few types have been added, so for many common types, it's substantially faster than using a stringstream. Second, it now checks the result, so (for example) if you convert from a string to an <code>int</code>, it can throw an exception if the string contains something that couldn't be converted to an <code>int</code> (e.g., <code>1234</code> would succeed, but <code>123abc</code> would throw).</p> <p>As of C++11, there's a <code>std::to_string</code> function overloaded for integer types, so you can use code like:</p> <pre><code>int a = 20; std::string s = to_string(a); </code></pre> <p>The standard defines these as being equivalent to doing the conversion with <code>sprintf</code> (using the conversion specifier that matches the supplied type of object, such as <code>%d</code> for <code>int</code>), into a buffer of sufficient size, then creating an <code>std::string</code> of the contents of that buffer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload