c++vector

how to increment twice in vector<pair<int, int>>


I have a vector of pair.

vector<pair<int, int>> myVector = {{X1, Y1}, {X2, Y2}, {X3, Y3}, {X4,Y4}, ....};

Lets say, I want to iterate over it such that a function deals with the X1,Y1,X2,Y2 at first iteration and X3,Y3,X4,Y4 in the second iteration and so on..

for (size_t i = 0; i < myVector.size(); i++) // i+2 works? 
    {
        //DrawLine(X1,Y1,X2,Y2);
    }

Thanks in advance. Please note auto keyword is not available in my IDE.


Solution

  • for (size_t i = 1; i < myVector.size(); i+=2) {
        auto X1 = myVector[i - 1].first;
        auto Y1 = myVector[i - 1].second;
        auto X2 = myVector[i].first;
        auto Y2 = myVector[i].second;
        //DrawLine(X1,Y1,X2,Y2);
    }