l00kitsjake
l00kitsjake

Reputation: 1025

Getting float value from struct that is inside a Vector

I have a struct Points that stores 4 floats. These structs are then put into a vector because I am storing points of a drawing (using OpenGL as well).

typedef struct {
    float x1, y1;                                                           
    float x2, y2;                                                           
} Points;

vector<Points> line;  
Points segment; 

I now have a function where my two vectors are the arguments and I want to be able to access each structures points (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;
}

How can I access each segment.x1 in the vectors?

I hope I am clear here and have provided enough info. I have tried outputting &line.front(); but that did not seem to work.

Upvotes: 0

Views: 154

Answers (2)

stas.yaranov
stas.yaranov

Reputation: 1787

for (Points& segment: line) {
    // here we can use segment.x1 and others
}

for (Points& segment: polygon) {
    // here we can use segment.x1 and others
}

This is called range-based for loop.

Upvotes: 1

Anon Mail
Anon Mail

Reputation: 4770

You can do something like this:

// 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

Upvotes: 0

Related Questions