It's a story about a car entering a queue at an intersection.
So, for each road, there are different lanes (iterator it
), and for each lanes, there are different cars (iterator it2
).
void function(Road& R, int timestep) {
vector<int> lane = R.void1() {
for(vector<int>::iterator it = lane.begin() ; it != lane.end() ; it++) {
vector<Car> cars = R.void2((*it));
for(vector<Car>::iterator it2 = cars.begin() ; it2 != cars.end() ; it2 ++) {
if((*it2).get_travel_time() >= R.get1((*it))
(*it2).init_travel_time();
else
(*it2).incr_travel_time(timestep);
}
}
}
where init_travel_time
sets the travel
of (*it)
to 0
and where incr_travel_time(timestep)
increments the same attribute, travel
, of (*it)
by timestep
.
The problem, which I see, is that the copy (*it2)
of the car is incremented, but not the car in the line R.void2((*it))
.
Instead, to increment directly on the car, I tried:
void function(Road& R, int timestep) {
for(vector<int>::iterator it = R.void1().begin() ; it != R.void1().end() ; it++) {
for(vector<Car>::iterator it2 = R.void2((*it)).begin() ; it2 != R.void2((*it)).end() ; it2 ++) {
if((*it2).get_travel_time() >= R.get1((*it))
(*it2).init_travel_time();
else
(*it2).incr_travel_time(timestep);
}
}
}
but I got the following error:
vector iterator incompatible
which is understandable (Vector Iterators Incompatible).
The fact is I think I cannot use the answers as long as my vector cannot be const (I change its attribute) and as long as the second answer get me back to my first proposition.