C++ 将 int 附加到 std::string

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10516196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:10:00  来源:igfitidea点击:

Append an int to a std::string

c++std

提问by Hakon89

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

回答by hmjd

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

std::string::append()方法期望它的参数是一个 NULL 终止的字符串 ( char*)。

There are several approaches for producing a stringcontaing an int:

有几种方法可以生成string包含 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));
    
  • 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));
    

回答by luke

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

字符串流参考

回答by Bojan Komazec

I have a feeling that your ClientIDis 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的不是字符串类型(以零结尾char*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();

回答by WeaselFox

You are casting ClientIDto 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 字符串)形成的字符串的副本。此字符序列的长度由空字符的第一次出现确定(由 traits.length(s) 确定)。