将int添加到std::string [duplicate]

时间:2022-09-02 09:05:17

This question already has an answer here:

这个问题已经有了答案:

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);

4 个解决方案

#1


175  

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

方法的参数是空终止字符串(char*)。

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

有几种方法可以产生一个与int型相接触的字符串:

  • std::ostringstream

    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::to_string(C + + 11)

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

    boost::lexical_cast

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

#2


5  

You cannot cast an int to a char* to get a string. Try this:

不能将int类型转换为char*来获取字符串。试试这个:

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

stringstream reference

stringstream参考

#3


2  

You are casting ClientID to char* causing the function to assume its a null terinated char array, which it is not.

您正在将ClientID转换为char*,导致函数假设它是一个空的三叉字符数组,但它不是。

from cplusplus.com :

从cplusplus.com:

string& append ( const char * s ); Appends a copy of the string formed by the null-terminated character sequence (C string) pointed by s. The length of this character sequence is determined by the first ocurrence of a null character (as determined by traits.length(s)).

string&append (const char * s);附加一个由s指向的空终止字符序列(C字符串)组成的字符串的副本。这个字符序列的长度由空字符的第一个ocrence(由.length(s)决定)决定。

#4


2  

I have a feeling that your ClientID is not of a string type (zero-terminated char* or std::string) but some integral type (e.g. int) so you need to convert number to the string first:

我感觉您的ClientID不是字符串类型(零终止字符*或std::string),而是某种整数类型(例如int),因此您需要首先将数字转换为字符串:

std::stringstream ss;
ss << ClientID;
query.append(ss.str());

But you can use operator+ as well (instead of append):

但是你也可以使用operator+(而不是append):

query += ss.str();

#1


175  

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

方法的参数是空终止字符串(char*)。

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

有几种方法可以产生一个与int型相接触的字符串:

  • std::ostringstream

    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::to_string(C + + 11)

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

    boost::lexical_cast

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

#2


5  

You cannot cast an int to a char* to get a string. Try this:

不能将int类型转换为char*来获取字符串。试试这个:

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

stringstream reference

stringstream参考

#3


2  

You are casting ClientID to char* causing the function to assume its a null terinated char array, which it is not.

您正在将ClientID转换为char*,导致函数假设它是一个空的三叉字符数组,但它不是。

from cplusplus.com :

从cplusplus.com:

string& append ( const char * s ); Appends a copy of the string formed by the null-terminated character sequence (C string) pointed by s. The length of this character sequence is determined by the first ocurrence of a null character (as determined by traits.length(s)).

string&append (const char * s);附加一个由s指向的空终止字符序列(C字符串)组成的字符串的副本。这个字符序列的长度由空字符的第一个ocrence(由.length(s)决定)决定。

#4


2  

I have a feeling that your ClientID is not of a string type (zero-terminated char* or std::string) but some integral type (e.g. int) so you need to convert number to the string first:

我感觉您的ClientID不是字符串类型(零终止字符*或std::string),而是某种整数类型(例如int),因此您需要首先将数字转换为字符串:

std::stringstream ss;
ss << ClientID;
query.append(ss.str());

But you can use operator+ as well (instead of append):

但是你也可以使用operator+(而不是append):

query += ss.str();