另外构建结构体时,有时为方便直接通过括号的方式进行,这样可以节省时间。如下所示:
#include<iostream>
#include<vector>
using namespace std;
struct mypoint
{
mypoint(double a, double b, double c)
{
this->x = a;
this->y = b;
this->z = c;
}
double x;
double y;
double z;
};
void main()
{
vector<mypoint> arr;
mypoint p1(1,1,1), p2(2,2,2), p3(3,3,3);
arr.push_back(p1);
arr.push_back(p2);
arr.push_back(p3);
for (int i = 0; i < arr.size(); i++)
{
cout << arr[i].x << "\t" << arr[i].y << "\t" << arr[i].z << endl;
}
system("pause");
}
相对来说效率更快点。但是这样有个问题,创建的结构体必须要初始化,不是很方便