C++ 从多个double和char C++;

C++ 从多个double和char C++;,c++,C++,我正试图用一对双打和一个字符组成一个字符串。我知道我这里的代码是错误的,但我认为它给出了我想做什么的想法 operand2 = A.pop(); //double operand1 = A.pop(); //double math = "-"; //char result = "%f %s %f",operand1, math, operand2; //string A.push(result); 我试着研究如何做到这一点。我不熟悉sprintf

我正试图用一对双打和一个字符组成一个字符串。我知道我这里的代码是错误的,但我认为它给出了我想做什么的想法

operand2 = A.pop();     //double
operand1 = A.pop();     //double
math = "-";             //char
result = "%f %s %f",operand1, math, operand2;     //string
A.push(result);
我试着研究如何做到这一点。我不熟悉sprintf和sprintcat,但这是最好的方法吗?非常感谢您的任何意见

试试这个尺码

您可以是特定的,并使用std::ostringstream等。但由于懒惰,此示例使用std::stringstream

#include <iostream>
#include <sstream>

// ...

double operand2 = A.pop();
double operand1 = A.pop();

std::stringstream stream;
stream << operand1 << " - " << operand2;
A.push(stream.str());
#包括
#包括
// ...
双操作数2=A.pop();
双操作数1=A.pop();
std::stringstream;
流
双操作数2=A.pop();
双操作数1=A.pop();
字符数学='-';
std::ostringstream oss;

OSS既然你使用C++,为什么不使用?如果你想使用C库,你就做一个合理的缓冲区<代码> char buff(50);<然后你
sprintf(buff,“%f%c%f”,操作数1,数学,操作数2)我不建议您也对
std::to_string
double operand2 = A.pop();
double operand1 = A.pop();
char math = '-';
std::ostringstream oss;
oss << operand1 << ' ' << math << ' ' << operand2;
std::string result = oss.str();
...