ฟังก์ชัน 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.