Program Tip

ostream을 표준 문자열로 변환

programtip 2020. 12. 2. 21:45
반응형

ostream을 표준 문자열로 변환


저는 C ++ STL을 처음 접했으므로 사소한 것일 수 있습니다. ostream텍스트 가있는 변수가 있습니다.

ostream* pout;
(*pout) << "Some Text";

스트림을 추출하여 유형의 문자열에 저장하는 방법이 char*있습니까?


     std::ostringstream stream;
     stream << "Some Text";
     std::string str =  stream.str();
     const char* chr = str.c_str();

그리고 한 시간 전에 쓴 질문 에 대한 답에서 무슨 일이 일어나고 있는지 설명합니다 .


질문은 ostream문자열이 아니라 문자열 에 관한 것이 었습니다 ostringstream.

실제 질문에 대한 답변에 관심이있는 분은 다음을 ostream시도해보십시오.

void someFunc(std::ostream out)
{
    std::stringstream ss;
    ss << out.rdbuf();
    std::string myString = ss.str();
}

시험 std::ostringstream

   std::ostringstream os;
   os<<"Hello world";
   std::string s=os.str();
   const char *p = s.c_str();

참고 URL : https://stackoverflow.com/questions/3513173/converting-ostream-into-standard-string

반응형