c++ - Best way to store vector of structures -
i need store sorted bunch of structures. best way in vector? should use pointers or make copy?
struct mystruct { int i; string str; //whatever... };
and then:
vector<mystruct> v;
or
vector<mystruct*> v;
thanks in advance.
storing raw pointers
vector<mystruct*> v;
is bad idea. supposed delete them? safer use smart pointer, e.g. in c++11
vector<std::shared_ptr<mystruct>> v;
or
vector<std::unique_ptr<mystruct>> v;
depending on doing. see here
if have simple value type, easier copy them i.e. suggest
vector<mystruct> v;
otherwise, if want oo polymorphism go pointer base in collection.
Comments
Post a Comment