C ++에서 stringstream에서 string으로 어떻게 변환합니까?
어떻게 변환 할 std::stringstream
로 std::string
C ++로?
문자열 스트림에서 메소드를 호출해야합니까?
yourStringStream.str()
.str ()-방법을 사용하십시오 :
기본 문자열 객체의 내용을 관리합니다.
1)를 호출하여 마치 기본 문자열의 복사본을 반환합니다
rdbuf()->str()
.2) 호출하여 마치 기본 문자열의 내용을 대체합니다
rdbuf()->str(new_str)
...노트
STR에 의해 반환 된 기본 문자열의 복사본을 이렇게 직접 호출 식의 말에 파괴 될 것이다 임시 객체 인
c_str()
의 결과str()
(예에auto *ptr = out.str().c_str();
매달려 포인터의) 결과 ...
std::stringstream::str()
찾고있는 방법입니다.
로 std::stringstream
:
template <class T>
std::string YourClass::NumericToString(const T & NumericValue)
{
std::stringstream ss;
ss << NumericValue;
return ss.str();
}
std::stringstream
보다 일반적인 도구입니다. std::ostringstream
이 특정 작업에 보다 전문화 된 클래스 를 사용할 수 있습니다 .
template <class T>
std::string YourClass::NumericToString(const T & NumericValue)
{
std::ostringstream oss;
oss << NumericValue;
return oss.str();
}
std::wstring
문자열 유형으로 작업하는 경우 선호 std::wstringstream
하거나 std::wostringstream
대신 해야합니다 .
template <class T>
std::wstring YourClass::NumericToString(const T & NumericValue)
{
std::wostringstream woss;
woss << NumericValue;
return woss.str();
}
문자열의 문자 유형을 런타임 선택 가능하게하려면 템플릿 변수로 만들어야합니다.
template <class CharType, class NumType>
std::basic_string<CharType> YourClass::NumericToString(const NumType & NumericValue)
{
std::basic_ostringstream<CharType> oss;
oss << NumericValue;
return oss.str();
}
위의 모든 방법에 대해 다음 두 헤더 파일을 포함해야합니다.
#include <string>
#include <sstream>
NumericValue
위 예제 의 인수 는 각각 및 인스턴스 와 함께 std::string
또는 인스턴스 std::wstring
와 함께 사용될 수도 있습니다 . 는 숫자 값일 필요는 없습니다 .std::ostringstream
std::wostringstream
NumericValue
메모리 stringstream::str()
에서 std::string
값 을 얻으려면 호출 합니다 .
참고URL : https://stackoverflow.com/questions/662976/how-do-i-convert-from-stringstream-to-string-in-c
'program tip' 카테고리의 다른 글
업로드하기 전에 이미지 미리보기 표시 (0) | 2020.07.28 |
---|---|
std :: swap ()을 오버로드하는 방법 (0) | 2020.07.28 |
C #에서 마우스 클릭을 어떻게 시뮬레이트합니까? (0) | 2020.07.28 |
파이썬은 여러 파일 형식을 가져옵니다 (0) | 2020.07.28 |
adb shell을 통해 활동을 시작할 수 있습니까? (0) | 2020.07.28 |