Vectors, iterators and loops

This is a discussion on Vectors, iterators and loops within the C++ Programming forums, part of the General Programming Boards category; I have a tile map each tile has its own class, each class is stored in a two dimensional array, ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    5

    Vectors, iterators and loops

    I have a tile map each tile has its own class, each class is stored in a two dimensional array, one of the things I store in the Tile class is how many ships are present each ship is assigned a Unique number to identify it. If there are more then 1 ship in a given Tile the program knows what one you wish to move (I pass the identifying number to the function). I have this piece of code that is supposed to find this ship that the player clicked on a move it to where they clicked however it only ever moves the first ship in the vector.

    Code:
    vector<int>::iterator MoveShip = Tile[OldMouseMapX][OldMouseMapY]->ShipsHere.begin();
    for(; *MoveShip < Tile[OldMouseMapX][OldMouseMapY]->ShipsHere.size();) {
        if(Tile[OldMouseMapX][OldMouseMapY]->ShipsHere[*MoveShip] == IShipID) {
            MoveShip = Tile[OldMouseMapX][OldMouseMapY]->ShipsHere.erase(MoveShip);
            Tile[MouseMap(X)][MouseMap(Y)]->ShipsHere.push_back(*MoveShip);
        }
        else {
            ++MoveShip;
        }
    }
    I know its passing along the right ship number and it is looping until it finds the ship but it will only ever move the first ship contained in the vector.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    In the for-loop you're comparing ID's with vector positions.
    could it be that you actually want
    Code:
    for(; MoveShip != Tile[OldMouseMapX][OldMouseMapY]->ShipsHere.end();) {
    Kurt

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    5
    Thanks Kurt but that didn't fixed the problem it works just as it always did

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21