2012-04-19 3 views
-1

가능한 중복 :
Easiest way to convert int to string in C++간단한 C++ - 문자열과 연결 및 변환 INT에 대한 문자열

나는 비주얼 C++ 문자열에 대한 질문이 있습니다. 다음 문자열을 연결하고 싶습니다.

#include<sstream> 

for (int i=0; i<23; i++) 
{ 
    stringstream left, right; 
    left << "C:/x/left" << i << ".bmp"; 
    right << "C:/x/left" << i << ".bmp"; 
    imagelist.push_back(left.str()); 
    imagelist.push_back(right.str()); 
} 

stringstream

는 빠른 성능의 솔루션이 아니라 이해하기 쉽고 매우 유연 :

for (int i=0; i<23; i++) 
{ 
    imagelist.push_back("C:/x/left"+i+".bmp"); 
    imagelist.push_back("C:/x/right"+i+".bmp"); 
} 

들으

답변

2
std::ostringstream os; 
os << "C:/x/left" << i << ".bmp"; 
imagelist.push_back(os.str()); 
2

하나의 해결책은 stringstream을 사용하는 것입니다.

또 다른 옵션은 C 스타일 인쇄로 가정에서 느끼는 경우 itoasprintf을 사용하는 것입니다. 그러나, 나는 그 itoa 매우 휴대 기능이 없다고 들었습니다.

2
for (int i=0; i<23; i++) 
{ 
    imagelist.push_back("C:/x/left"+std::to_string(i)+".bmp"); 
    imagelist.push_back("C:/x/right"+std::to_string(i)+".bmp"); 
}