sprintf的C ++等效项是什么?
该sprint()函数也存在于C和C++内部。此函数用于将某些内容存储在字符串中。语法就像printf()函数一样,唯一的区别是,我们必须在其中指定字符串。
同样在C++中,我们可以通过使用ostringstream进行相同的操作。此ostringstream基本上是输出字符串流。这存在于sstrem头文件中。让我们看看如何使用它。
示例
#include<iostream>
#include<sstream>
using namespace std;
int main() {
string my_str;
ostringstream os;
os << "This is a string. We will store " << 50 << " in it. We can store " << 52.32 << " also.";
my_str = os.str(); //now convert stream to my_str string
cout << my_str;
}输出结果
This is a string. We will store 50 in it. We can store 52.32 also.
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短