Appending Integer Values to a String in C
Originally, this question sought to address the issue of appending an integer to a string, but encountered a runtime error. The C programming language offers several methods for resolving this issue.
std::string::append()
The std::string::append() method expects arguments to be null-terminated strings (char*). However, directly appending an integer (int) will not produce the desired result.
Recommended Approaches
To append an integer value to a string, consider the following techniques:
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));
These methods effectively convert the integer value into a string representation and append it to the string.
The above is the detailed content of How to Append an Integer Value to a String in C ?. For more information, please follow other related articles on the PHP Chinese website!