如何初始化PythONLY列表并在C++中写入文件

2024-10-16 20:43:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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))

Tags: 文件to代码name列表deflineex
1条回答
网友
1楼 · 发布于 2024-10-16 20:43:57

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

int dor=5,ex=10,sx=20,sy=30,ey=50;
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] <<std::endl;
}
myfile.close();

相关问题 更多 >