struct Vec2 { float x, y; Vec2(float x=0,float y=0):x(x),y(y){} };
struct Vec3 { float x, y, z; Vec3(float x=0,float y=0,float z=0):x(x),y(y),z(z){} };
struct Obj { vector tex; vector vert; };
Obj obj;
obj.tex.push_back(Vec2(0.1, 0.2));
obj.tex.push_back(Vec2(0.2, 0.7));
obj.vert.push_back(Vec3(0.0, 0.5, 0.8));
obj.vert.push_back(Vec3(0.7, 0.67, 0.89));
for (vector::iterator it = obj.tex.begin(); it != obj.tex.end(); ++it) {
cout << it->x << " " << it->y << endl;
}
for (auto it = obj.tex.begin(); it != obj.tex.end(); ++it) {
cout << it->x << " " << it->y << endl;
}
for (size_t i = 0; i < obj.tex.size(); ++i) {
cout << obj.tex[i].x << " " << obj.tex[i].y << endl;
}
for (auto& v : obj.tex) { // 引用避免拷貝
cout << v.x << " " << v.y << endl;
}
// 或值語義(僅讀)
for (auto v : obj.tex) { cout << v.x << " " << v.y << endl; }
<iterator>auto&;僅讀用 const auto& 或值熟練掌握 vector 遍歷是 C++ 基礎功底。