Append an int to a std::string

c++, std

Solution

The `std::string::append()` method expects its argument to be a NULL terminated string (`char*`).

There are several approaches for producing a `string` containg an `int`:

`std::ostringstream`

#include <sstream>

std::ostringstream s;
s << "select logged from login where id = " << ClientID;
std::string query(s.str());

`std::to_string` (C++11)

std::string query("select logged from login where id = " +
                  std::to_string(ClientID));

`boost::lexical_cast`

#include <boost/lexical_cast.hpp>

std::string query("select logged from login where id = " +
                  boost::lexical_cast<std::string>(ClientID));

Problem

Why is this code gives an Debug Assertion Fail? ``` std::string query; int ClientID = 666; query = "select logged from login where id = "; query.append((char *)ClientID); ```

Original source

Related problems