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

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -