James Wilks
James Wilks

Reputation: 740

Inserting into Containers

This is probably a dumb question I just need some suggestions. I have a vector of vector of ints called map which holds an integer value and obviously has a location that will correspond to a location on a map. What I'm trying to do is create a deque of structs called gridLoc

struct gridLoc{
    int x;
    int y;
    int rubble;
};

What I've discovered is you can't create a struct gridLoc x, push it onto the deque, then change that x and push it on the deque again and have two different structs in the deque. What would be the best way to get any amount of different structs inside the deque? Would i need to create an array of structs and just insert differ indices of the array. My class is big on time and memory so I'm trying to think of the best way to do this.

Upvotes: 0

Views: 71

Answers (2)

MiXaeL
MiXaeL

Reputation: 21

Actually, since all insert functions do a copy of inserted items, you can do exactly what you are trying to do. Check if you're trying to push actual objects, not pointers.

Upvotes: 1

Bill Lynch
Bill Lynch

Reputation: 81936

This works just fine:

#include <deque>

struct gridLoc{
    int x;
    int y;
    int rubble;
};

int main() {
    std::deque<gridLoc> locations;

    for (int i=0; i<10; ++i) {
        gridLoc x = {i, i, i%2};
        locations.push_back(x);
    }
}

Upvotes: 1

Related Questions