这是我在Stackoverflow上的第一个问题。我正在使用C ++在GP142上创建一个绘图程序,而我在重新创建画布时遇到了麻烦。 加载程序时,它会询问点(或包含x和y的顶点)并通过连接所有这些点来创建形状(这是一个类)。我将点保存在一个点*向量中:
---
- hosts: test-server
tasks:
- stat:
path: "{{ item }}"
checksum_algorithm: sha1
delegate_to: localhost
with_fileglob: /tmp/*.dat
register: local_files
- stat:
path: "/tmp/{{ item.stat.path | basename }}"
checksum_algorithm: sha1
failed_when: remote_files.stat.checksum != item.stat.checksum
# failed_when condition checked after every iteration
# and remote_files here is a result of individual task
# but after loop is finished, remote_files is a cobination
# of all iterations results
with_items: "{{ local_files.results }}"
register: remote_files
loop_control:
label: "{{ item.stat.path | basename }}"
当形状完成时,它应该保存在矢量数组中。我使用以下方式保存它:
vector<shape*> arr;
有5种不同的形状类型,都是在while(true)循环中创建的,它终止于菜单选择。从菜单中选择撤消时,将删除最后创建的形状。 我使用以下方法删除它:
line l(points,2); //2 is num of points
l.draw();
arr.push_back(&l);
当我撤消时,它会删除所有相同类型的形状。就像它删除我创建的所有行一样。有没有什么方法可以区分所有行和其他变量,以便可以明确删除它们?
谢谢。
答案 0 :(得分:1)
我看到了几个问题。首先,你在while循环中创建line
,所以当循环结束并且你的行超出范围时,向量中的所有指针都将是悬空指针,即指向line
s已被摧毁。您可以根据自己的要求使用std::unique_ptr
或std::shared_ptr
来避免此问题。假设你选择了shared_ptrs,那么你的矢量类型应该是std::vector<std::shared_ptr<shape>>
,并且你可以在循环中使用
auto a_line = std::make_shared<line>(points,2); //2 is num of points
a_line->draw();
arr.push_back(a_line);
其次,要删除向量的最后一个元素,只要数组不为空,您需要做的是arr.pop_back()
。
答案 1 :(得分:0)
删除向量#include<iostream>
#include<fstream>
#include<string.h>
#include<iomanip>
using namespace std;
const int buffSize=250;
class StudentData
{
char rollNo[12];
char firstName[16];
char secondName[16];
char division[8];
char address[32];
char buffer[buffSize];
//makes data more readable and copies it to buffer,buffer written as it is into a file
void pack()
{
strcpy(buffer,"| ");
strcat(buffer,"ROLL NO: ");
strcat(buffer,rollNo);
strcat(buffer," ");
strcat(buffer,"| ");
strcat(buffer,"NAME: ");
strcat(buffer,firstName);
strcat(buffer," ");
strcat(buffer,secondName);
strcat(buffer," ");
strcat(buffer,"| ");
strcat(buffer,"DIVISION: ");
strcat(buffer,division);
strcat(buffer," ");
strcat(buffer,"| ");
strcat(buffer,"ADDRESS: ");
strcat(buffer,address);
strcat(buffer," ");
strcat(buffer," |");
}
//data from file is available in buffer, copied into respective variables for display
void unpack()
{
strtok(buffer," ");
strtok(NULL," ");
strtok(NULL," ");
strcpy(rollNo,strtok(NULL," |"));
strtok(NULL," ");
strtok(NULL," ");
strcpy(firstName,strtok(NULL," "));
strcpy(secondName,strtok(NULL," |"));
strtok(NULL," ");
strtok(NULL," ");
strcpy(division,strtok(NULL," |"));
strtok(NULL," ");
strtok(NULL," ");
strcpy(address,strtok(NULL," |"));
}
public:
//add data to file
void add_data()
{
cout<<"Enter Roll No: ";
cin>>rollNo;
cout<<endl;
cout<<"Enter Name of student: ";
cin>>firstName;
cin>>secondName;
cout<<endl;
cout<<"Enter Division of student: ";
cin>>division;
cout<<endl;
cout<<"Enter Address of student: ";
cin>>address;
cout<<endl;
//pack data
pack();
//create ofstream object for writing in file
ofstream outFile;
//open in append mode
outFile.open("Student Data",ios::app);
//write contents of buffer to file and a new line
outFile<<buffer<<endl;
//close file
outFile.close();
cout<<"Data successfully added to file...!"<<endl;
}
void display()
{
cout<<"-------------------------------------------"<<endl;
cout<<endl;
cout<<"**********Student Information************"<<endl;
cout<<"-------------------------------------------"<<endl;
cout<<endl;
cout<<setw(12)<<"Roll No";
cout<<setw(30)<<"Name";
cout<<setw(30)<<"Division";
cout<<setw(30)<<"Address";
cout<<endl;
//create ifstream object to read from file
ifstream inFile;
//open file
inFile.open("Student Data");
while(!inFile.eof())
{
//read data from file into buffer
inFile>>buffer;
if(inFile.fail())
{
cout<<"File failed to open"<<endl;
break;
}
unpack();
cout<<rollNo<<setw(12);
cout<<firstName<<setw(14);
cout<<secondName<<setw(1);
cout<<division<<setw(30);
cout<<address<<setw(30);
}
//close file
inFile.close();
}
};
int main()
{
StudentData s;
s.add_data();
s.display();
return 0;
}
的最后一个元素:
arr