C++ - How do I insert multiple values into a vector, each with a different index? -
currently have vector holding references rooms connected specific room:
rooms.at(1).connectedrooms = { &rooms.at(2), &rooms.at(3), &rooms.at(4), &rooms.at(5)};
however use enum index, rather integer, instead of rooms.at(1).connectedrooms.at(0)
use rooms.at(1).connectedrooms.at(north)
makes more sense , allows used more , legibly in switch statements.
the enum looks like
enum directions { north, east, south, west};
is there way on 1 line, example (this code doesn't work, it's do):
rooms.at(1).connectedrooms[north, east, south, west] = {&rooms.at(2), &rooms.at(3), &rooms.at(4), &rooms.at(5)};
rather longer
rooms.at(1).connectedrooms[north] = &rooms.at(2); rooms.at(1).connectedrooms[east] = &rooms.at(3); rooms.at(1).connectedrooms[south] = &rooms.at(4); rooms.at(1).connectedrooms[west] = &rooms.at(5);
thanks
you can assign integer values enum constants:
enum directions { north = 0, east = 1, south = 2, west = 3 };
and keep assignment is:
rooms.at(1).connectedrooms = { &rooms.at(2), &rooms.at(3), &rooms.at(4), &rooms.at(5)};
edit: per comment below, please aware way holding pointers vector members dangerous , not considered practice. remember std::vector dynamic, may decide change location of memory area anytime needs to, such when grows...
Comments
Post a Comment