string이랑 int를 붙이고 싶어요
조회수 2476회
쉬울줄 알았는데 어렵네요 ㅜㅜㅜ
아래 코드에서 string name
이 "John21"
이 되게 하려면 어떻게 해야할까요
소스코드
string name = "John";
int age = 21;
1 답변
-
알파벳 순서대로 적겠습니다.
사용법
// 1. Boost
result = name + boost::lexical_cast<std::string>(age).
// 2. FastFormat.Format
fastformat::fmt(result, "{0}{1}", name, age);
// 3. FastFormat.Write
fastformat::write(result, name, age);
// 4. IOStreams
std::stringstream sstm; sstm << name << age; result = sstm.str();
// 5. itoa
char numstr[21]; // enough to hold all numbers up to 64-bits result = name + itoa(age, numstr, 10);
// 6. sprintf
char numstr[21]; // enough to hold all numbers up to 64-bits sprintf(numstr, "%d", age); result = name + numstr;
// 7. STLSoft's integer_to_string
char numstr[21]; // enough to hold all numbers up to 64-bits result = name + stlsoft::integer_to_string(numstr, 21, age);
// 8. STLSoft's winstl::int_to_string()
result = name + winstl::int_to_string(age);
// 9. Poco NumberFormatter
result = name + Poco::NumberFormatter().format(age);
설명
- 안전하고 느린방법. Boost헤더가 필요함. 모든 플랫폼에서 동작
- 안전하고 빠른방법. FastFormat이 필요함. 모든 플랫폼에서 동작
- 안전하고 빠른방법. FastFormat이 필요함. 모든 플랫폼에서 동작
- 안전하고 느린방법. #include 해야. 은 c++ standard이므로 모든 플랫폼에서 동작
- 불안하고 빠른방법. 큰 버퍼가 필요, itoa()는 standard가 아니기 때문에 모든 플랫폼에서 동작할거란 보장이 없음
- 불안하고 빠른방법. 큰 버퍼가 필요, c++ standard이므로 모든 플랫폼에서 동작.
- 불안하고 가장빠른방법. 큰 버퍼가 필요, STLSoft가 필요함. 모든 플랫폼에서 동작
- 가장안전하고 빠른 방법. STLSoft가 필요함. 윈도우에서만 동작
- 안전하고 느린방법. Poco C++이 필요. 모든 플랫폼에서 동작
댓글 입력