本文介绍了将双面转换为字符串C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想合并一个字符串和一个double和g ++是抛出这个错误:I want to combine a string and a double and g++ is throwing this error: main.cpp:In函数'int main()': main.cpp:40:错误:类型为'const char [2]'和'double'的操作数为二进制'operator +'main.cpp: In function ‘int main()’:main.cpp:40: error: invalid operands of types ‘const char [2]’ and ‘double’ to binary ‘operator+’这是代码行,它是抛出的错误:Here is the line of code which it is throwing the error on:storedCorrect[count] = "("+c1+","+c2+")"; storedCorrect []是一个字符串数组,c1和c2都是双精度。有没有办法将c1和c2转换为字符串以允许我的程序正确编译?storedCorrect[] is a string array, and c1 and c2 are both doubles. Is there a way to convert c1 and c2 to strings to allow my program to compile correctly?推荐答案直。有很多方法可以做到:You can't do it directly. There are a number of ways to do it: 使用 std :: stringstream :std::ostringstream s;s << "(" << c1 << ", " << c2 << ")";storedCorrect[count] = s.str() 使用 boost :: lexical_cast :storedCorrect[count] = "(" + boost::lexical_cast<std::string>(c1) + ", " + boost::lexical_cast<std::string>(c2) + ")"; 使用 std :: snprintf :char buffer[256]; // make sure this is big enough!!!snprintf(buffer, sizeof(buffer), "(%g, %g)", c1, c2);storedCorrect[count] = buffer; 各种双字到字符串转换函数,但这些是您将看到它完成的主要方式。There are a number of other ways, using various double-to-string conversion functions, but these are the main ways you'll see it done. 这篇关于将双面转换为字符串C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-12 22:34