-1

I want to iterate over a vector of an custom structure "Edge". I have sorted the vector beforehand for the attribute weight. Sometimes I want to stop iterating over the vector, calculate other stuff. When I iterate over the vector again, I want to start at the last visited element.

Example with Integers: Edge : [0,1,2,3,4,5,6] Iterate: 0,1,2 Break out of function and calculate other stuff Iterate: [3,4,5,6]

I thought about saving a vector::iterator. It would be easier to change the pointer to the first element to the wanted element instead but I didn't found anything for this

Thanks.

Zwuedus
  • 1
  • 1

1 Answers1

1

Try something like this:

for(int i = 0; i < vec.size(); i++) {
     do_work(vec[i]);
     if(i == breakPoint) {
         do_calculations();
     }
}

Where breakPoint is an int indicating the index at which you want to stop. Unless, breaking the function is really important. Then you could use a static i, but that has the potential to give you more of a headache than I think it's worth

Gabriel
  • 829
  • 5
  • 12