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