我如何初始化类似python的列表并写入C ++中的文件

我在python中有此代码,我必须在c ++中进行翻译,但是我被困住了,因为我无法弄清楚如何无法在python中初始化像这样的列表以及如何在类似以下代码的文件中写入

def to_file(sy, ey, sx, ex, name, dor):
    rBloc = ["LINE\n", "8\n", str(dor) + "\n",
             "10\n" + str(sx) + "\n" + "11\n" + str(ex) + "\n" +
             "20\n" + str(sy) + "\n" + "21\n" + str(ey) + "\n" +
             "0\n"]
    with open(files.result_directory + r"\box_" + name[:-1] + ".dxf", "a") as f:
        for i in rBloc:
            f.write(str(i))
评论
  • 柠檬心
    柠檬心 回复

    您可以使用C ++向量替换python中的列表。整个代码将如下所示。

    vector<std::string> rBloc{ "LINE\n", "8\n", std::to_string(dor) + "\n",
                 "10\n" + std::to_string(sx) + "\n" + "11\n" + std::to_string(ex) + "\n" +
                 "20\n" + std::to_string(sy) + "\n" + "21\n" + std::to_string(ey) + "\n" +
                 "0\n" };
    
    ofstream myfile;
    myfile.open ("example.txt");
    for(int i=0;i<rBloc.size();i++) {
        myfile << rBloc[i];
    }
    myfile.close();