可能重複:
Easiest way to convert int to string in C++簡單的C++ - 關於字符串和級聯和INT轉換爲字符串
我有一個關於Visual C++字符串問題。我想連接下一個字符串。
for (int i=0; i<23; i++)
{
imagelist.push_back("C:/x/left"+i+".bmp");
imagelist.push_back("C:/x/right"+i+".bmp");
}
THX
可能重複:
Easiest way to convert int to string in C++簡單的C++ - 關於字符串和級聯和INT轉換爲字符串
我有一個關於Visual C++字符串問題。我想連接下一個字符串。
for (int i=0; i<23; i++)
{
imagelist.push_back("C:/x/left"+i+".bmp");
imagelist.push_back("C:/x/right"+i+".bmp");
}
THX
std::ostringstream os;
os << "C:/x/left" << i << ".bmp";
imagelist.push_back(os.str());
一種解決方案是使用stringstreams:
#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
不在性能解決方案的速度快,但很容易理解和非常靈活的。
另一種選擇是使用itoa
和sprintf
,如果你感覺在家用c式打印。不過,我聽說itoa
不是很便攜的功能。
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");
}