0

我有一个 c++ 程序,我在其中多次运行相同的函数,但每次都运行不同的参数值。对于参数的每个值,我想将函数的结果输出到名称包含参数值的文件中。这该怎么做?这是我想做的一个例子。

for(parameter = 10;parameter<=100;parameter*=10){
       ofstream file("file"<<parameter<<".txt", ios::out);
       function();
       file<<result;
       file.close();
}        
4

1 回答 1

4

你可以这样做ostringstream

for (int parameter = 10; parameter <= 100; paramter *=10 )
{
    std::ostringstream name;
    name << "file" << parameter << ".txt";

    // If your library is too old, you have to use
    // name.str().c_str()
    // to get the string
    std::ofstream file(name.str()); // or name.str().c_str() in C++03

    // ...
}
于 2012-05-29T12:19:22.913 回答