首先定義三個結構體,注意初始化:
struct CVector2{
struct CVector2()
{
x = 0.0;
y = 0.0;
};
float x;
float y;
};
struct CVector3{
struct CVector3()
{
x = 0.0;
y = 0.0;
z = 0.0;
};
float x;
float y;
float z;
};
struct obj{
vector
vector
};
然後介紹遍歷方式:
obj obbject;
CVector2 elem2;
CVector3 elem3;
elem2.x = 0.1;
elem2.y = 0.2;
elem3.x = 0.0;
elem3.y = 0.5;
elem3.z = 0.8;
obbject.TexCoord.push_back(elem2);
elem2.x = 0.2;
elem2.y = 0.7;
elem3.x = 0.7;
elem3.y = 0.67;
elem3.z = 0.89;
obbject.TexCoord.push_back(elem2);
//遍歷方式1,采用迭代器
for (vector
{
cout << (*it).x << endl;
cout << (*it).y<
//遍歷方式2,采用迭代器.並采用C++11新標准中的auto關鍵字
for (auto it = obbject.TexCoord.begin(); it != obbject.TexCoord.end();it++)
{
cout << (*it).x << endl;
cout << (*it).y << endl;
}
////遍歷方式3,采用下角標進行數據元素訪問
for (size_t i = 0; i < obbject.TexCoord.size(); i++)
{
cout << obbject.TexCoord[i].x << endl;
cout << obbject.TexCoord[i].y << endl;
}
//遍歷方式4,采用C++11新標准中的auto關鍵字
for (auto i:obbject.TexCoord)
{
cout << i.x << endl;
cout << i.y << endl;
}
system("pause");
注意:vector容器使用頻率很高,應該熟練掌握!