3

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.

Community
  • 1
  • 1
Igor OA
  • 387
  • 3
  • 13
  • please give us a more detalied error log. btw: remember that `(*ptr).` is equivalent to `ptr->` – dlavila May 27 '15 at 14:34
  • Probably your `R.void1()` and `R.void2((*it))` both methods return copies but not references so you are iterating over the copies of the objects. Your code is very dirty honestly. Please show implementations of this methods. – VP. May 27 '15 at 14:39
  • @VictorPolevoy : I'm pretty new on C++ and autodidact. So I would be glad to understand why my code is dirty and what are the possible ways to make it better (Actually, the following `function`, `void1`, `void2` ... do not have such names) – Igor OA May 27 '15 at 15:17

1 Answers1

3

To change actual vector contained in Road and avoid creating copy, create a references to a vectors in your first version:

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);
        }
    }
}

Also, void1 and void2 should return references (if they are not)

vector<int>& Road::void1();
vector<Car>& Road::void2(int lane);

I also removed redundant brackets in your code to simplify reading.

Ilya Kobelevskiy
  • 5,245
  • 4
  • 24
  • 41