C++ 中 sprintf 的等价物是什么?
C 和 C++ 中都有 sprint() 函数。此函数用于将内容存储在字符串中。其语法与 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.
广告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP