我做了一些研究,没有找到具体的答案,是否可以增加变量名称和/或文件位置? 是否有可能做这样的事情:
for(int i = 0; i<5; i ++)
{
name1->readName("filelocation/name1.txt");
}
并在每个循环周期中将name1
和name1.txt
增加一个?那么在secon循环name1
变为name2
?
感谢您提前提供任何帮助
答案 0 :(得分:6)
变量名称 - 没有。表示文件名的字符串文字 - 是的。使用std::to_string函数的简单示例:
#include <iostream>
#include <string>
void foo(const std::string& s) {
std::cout << s << '\n';
}
int main() {
for (int i = 0; i < 5; i++) {
foo("filelocation/name" + std::to_string(i) + ".txt");
}
}
答案 1 :(得分:0)
您可以使用:
<div class="container">
<div class="box"></div>
<div class="box2"></div>
<div class="box2"></div>
</div>
答案 2 :(得分:0)
你可以这样做:
std::string strExt = ".txt";
std::string strPath = "D:\\Data\\test";
std::string strFullPath = strPath + strExt;
for(int i(0); i < 5; i++){
std::cout << strFullPath << std::endl;
strPath[strPath.length() - 1]++;
strFullPath = strPath + strExt;
}
但是你可以看到循环的时间稍长一些,你就会让它循环回来。所以要做一些更大的事情然后只增加到最后一个字符或数字然后添加新的字母等等......
准确!您没有递增variable name
,而是递增变量值。所以我在我的代码中增加了strPath
而不是strPath
本身的值(“strPath,strPati,strPatj,strPatk ...”)。因为c ++不允许这样的事情。
答案 3 :(得分:0)
您应该将这些对象存储在数组,向量等容器中,以便使用索引而不是名称序列访问每个对象。假设你有一个名为Name
的对象,这是我用C ++方式的替代答案:
std::vector<Name> name;
int N = 3;
for(int i = 0; i < N; i++) {
std::stringstream ss;
ss << "filelocation/name" << i << ".txt";
img.push_back(Name());
img[i].readName(ss.str());
}