stringstream and for loop
stringstream and for loop
我使用stringstream来生成"动态",我需要打开的文件的名称,这是我的代码:
for (int img=0; img<5; img++)
{
stringstream stream;
string *s=new string("myfile");
stream << img << ".png"
s->append(stream.str());
.. other code
的问题是比第一次程序流入循环它工作得很好,第二个时间流没有值"1.png",但有值null…所以当我试图打开文件时,我得到一个空指针。
我怎么解决这个问题?
一个更简单的解决方案:
for (int img = 0; img < 5; ++img)
{
std::string s = "myfile" + ('0' + img) + ".png";
// do something useful with s
}
如果数字大于9,可以使用std::to_string(img)
尝试在循环之前分配字符串。
string *s = new string("myfile");
for(;;;){} //forloop
//在这里使用s
delete s;
//总是删除动态分配的内存
相关文章:
- 为什么我不能在 FOR LOOP 中使用 i/10,C++?
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- "Ill-defined for-loop - loop executes infinitely" (MSVC C6295)
- For-loop或std::any_of,我应该使用哪一个?
- 在"for loop"中使用GOTOXY定位
- 在这种情况下,我将如何继续 1 到 360 的"for loop"处理指针和地址?
- C++ For Loop在过程中被跳过了.如何解决?
- 如何增加"for loop"呈现的结果?
- for(;;) vs do..while() for main program loop
- 在C 中,如何在使用for loop的整数中找到所有连续的复合编号
- C :用sizeof()operator在for for loop中增加一个int变量,只能工作一次
- Bjarne Stroustrup Book - Vector and For loop - 将不起作用
- OpenCV cv::Mat "+=" inside for loop
- exc_bad_access on for loop(数组功能指针)
- For-Loop帮助获得错误的输出
- "Do not change a loop variable inside a for loop block"的意义是什么?
- paralelizing for loop with inequality (openmp c++)
- C++ EXPECT_EQ & ASSERT inside for loop
- C++迭代器"for loop"习惯用法,其步长> 1 并允许非随机访问反向迭代器
- Cplex c++ loop for