Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

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.