从Vector内部的结构中获取浮点值
Getting float value from struct that is inside a Vector
我有一个存储4个浮点的结构体Points。然后将这些结构放入向量中,因为我正在存储图形的点(也使用OpenGL)。
typedef struct {
float x1, y1;
float x2, y2;
} Points;
vector<Points> line;
Points segment;
我现在有一个函数,其中我的两个向量是自变量,我希望能够访问每个结构点(x1,x2,y1,y2)
int CyrusBeckClip (vector<Points>& line, vector<Points>& polygon) {
// How can I access each segment.x1 in the vector?
// (I reuse the segment instance for each line drawn)
return 0;
}
如何访问矢量中的每个segment.x1?
我希望我在这里很清楚,并提供了足够的信息。我尝试过输出&line.front();
,但似乎不起作用。
for (Points& segment: line) {
// here we can use segment.x1 and others
}
for (Points& segment: polygon) {
// here we can use segment.x1 and others
}
这被称为基于范围的for循环。
您可以这样做:
// using an index
line[0].x1;
// using an iterator
std::vector<Points>::iterator = line.begin();
line_iter->x1; // access first element's x1
(*line_iter).x1 // access first element's x1
// using front
line.front().x1 // access first element's x1
相关文章:
- 从字符串中获取结构的大小
- 获取结构 c++ 中元素的索引
- 如何获取结构C++的输入
- 使用加速融合获取结构名称
- 如何使用值获取结构的索引
- 如何获取结构体成员的地址
- 从行分析中获取结构
- 获取结构的 UUID
- 如何从 QMetaType 获取结构成员的名称
- 在发送/获取结构时使用 streambuf
- 无法获取结构 C# 的指针
- 如何在LLVM中获取结构成员的值?
- 如何从命名管道获取结构化数据并将其写入Qt中的qtableview
- 如何获取结构的元素作为类型提升::shared_ptr
- C++-如何获取结构的最后一个成员类型以及从现有对象访问它的方法
- 在c++中获取结构的参数名称
- 当我尝试获取结构成员的地址时,会发生奇怪的事情
- 获取结构数组 c++ 中的元素数
- 如何获取结构或类的尾随填充的大小
- 正在获取结构的实际大小