c++ - Most efficient way to move row/column in 2D std::vector -
i'm creating game application in c++. have map represented 2-dimensional std::vector
of tile
objects.
i need update map player moves. server application row or column new part of global map, should placed in local client's map, example:
in figure 1 there's local map before player moves. top row filled objects 1, center 2 , bottom 0. when player moves up, new top row filled objects 3 , others should go down, , previous bottom row should disappear.
i can moving required objects in for
loops, thinking if there's kind of algorithm in standard library or prefered many, efficient way achieve kind of modifications.
edit:
sorry didn't realize there difference between doing operation row , column, indeed there is. editted title, because need column too.
you might want implement iterator , don't move elements of vectors @ all. define variable index of top row (on screen), use modulo operator iterate on rows (so 000 row should overwritten 333, , top row index 2 instead of 0). algorithm effecient (only many memory writes needed), , used scroll in direction:
- moving upwards: decrement top row index (mod row number), change last row
- moving downwards: increment top row index (mod row number), change first row
- moving left: decrement left col index (mod col number), change last col
- moving right: increment left col index (mod col number), change first col.