IT박스

C ++ 1 문자에서 문자열로 변환?

itboxs 2020. 8. 12. 07:58
반응형

C ++ 1 문자에서 문자열로 변환? [닫은]


정말 가까운 답을 찾지 못했습니다 ...

반대의 방법은 str [0]처럼 매우 간단합니다.

하지만 문자열에 1 문자 만 캐스팅하면됩니다 ...

이렇게 :

char c = 34;
string(1,c);
//this doesn't work, the string is always empty.

string s(c);
//also doesn't work.

boost::lexical_cast<string>((int)c);

//also return null

모든

string s(1, c); std::cout << s << std::endl;

std::cout << string(1, c) << std::endl;

string s; s.push_back(c); std::cout << s << std::endl;

나를 위해 일했습니다.


나는 솔직히 캐스팅 방법이 잘 될 것이라고 생각했습니다. 그렇지 않기 때문에 stringstream을 시도해 볼 수 있습니다. 예는 다음과 같습니다.

#include <sstream>
#include <string>
stringstream ss;
string target;
char mychar='a';
ss << mychar;
ss >> target;

참고 URL : https://stackoverflow.com/questions/17201590/c-convert-from-1-char-to-string

반응형