如何初始化一个 python-like 列表并写入 C++ 中的文件
How can I initialize a python-like list and write to a file in 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 中的列表。整个代码如下所示。
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();
我在 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 中的列表。整个代码如下所示。
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();