Simple C++ - about strings and concatenation and converting int to string
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
Easiest way to convert int to string in C++
我有一个关于VisualC++字符串的问题。我想连接下一个字符串。
1 2 3 4 5 | for (int i=0; i<23; i++) { imagelist.push_back("C:/x/left"+i+".bmp"); imagelist.push_back("C:/x/right"+i+".bmp"); } |
谢谢
1 2 3 4 5 | 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"); } |
一种解决方案是使用StringStreams:
1 2 3 4 5 6 7 8 9 10 | #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()); } |
另一种选择是使用
1 2 3 | std::ostringstream os; os <<"C:/x/left" << i <<".bmp"; imagelist.push_back(os.str()); |