c++对象数组
首先定义一个简单的类:
class Section{
public:
Section(double top, double bottom){this->top = top; this->bottom = bottom;}
Section(){}
double top;
double bottom;
};
将对象放入数组类型中
Section b[3];
b[0] = Section(1000, 1007);
b[1] = Section(1010, 1020);
b[2] = Section(1030, 1040);
for(int i=0; i<3; i++){
cout << b[i].top << "," << b[i].bottom << endl;
}
将对象放入vector类型中
vector<Section *> data;
data.push_back(new Section(1000, 1007));
data.push_back(new Section(1010, 1020));
data.push_back(new Section(1030, 1040));
for(int i=0; i<3; i++){
cout << data[i]->top << "," << data[i]->bottom << endl;
}
for(int i=0; i<data.size(); i++){
delete data[i];
}
在c++中使用完指针要进行delete释放。