int a1 = 1;
int a2 = 10;
char *sqliteInsert = "insert into test123 values("+a1+","+a2+")";
上面的写法是错误的。问题是c++中有没有类似Java中tostring的用法。
int a1 = 1;
int a2 = 10;
char *sqliteInsert = "insert into test123 values("+a1+","+a2+")";
上面的写法是错误的。问题是c++中有没有类似Java中tostring的用法。
嗯,你其实是打算问,在C++中怎么把int类型数据转换成string吧?
我觉得最简单的办法,就是用std::stringstream
#include <sstream>
std::stringstream query;
query << "insert into test123 values(" << a1 << ", " << a2 << ")";
char *sqliteInsert = query.str().c_str();
更新:我这段代码有问题,query.str().c_str()
返回的结果应该进行const_cast
,见@pezy 的答案。
3 回答2k 阅读✓ 已解决
2 回答3.9k 阅读✓ 已解决
2 回答3.2k 阅读✓ 已解决
1 回答3.2k 阅读✓ 已解决
1 回答2.7k 阅读✓ 已解决
1 回答2.4k 阅读✓ 已解决
3 回答3.5k 阅读
char* / const char*
类型不支持+
操作符。你应该使用std::string
.Java
中toString
的用法? 有。std::to_string
方法。stringstream
,如 @spacewander 的答案。c++11:
c++0x:
注意:
std::string::c_str()
返回值是const char *
,所以直接将其赋值给char *
,编译器可能会给出 error。安全的方式是对其进行const_cast
.