0

I need to store a sorted bunch of structures. What is the best way to do it in a vector? Should I use pointers for this or make a copy?

struct myStruct {
    int i;
    string str;
    //whatever...
};

and then:

vector<myStruct> v;

or

vector<myStruct*> v;

Thanks in Advance.

skater_nex
  • 453
  • 1
  • 5
  • 11
  • 8
    Store values unless you have good reasons not to do so. – juanchopanza Aug 16 '13 at 10:42
  • Thanks. Just wander in what cases using of pointers should be ok. Thought that using pointers should minimize memory usage. Sorry for noob question - new to c++ – skater_nex Aug 16 '13 at 10:49
  • 1
    How would it minimize memory usage? – HAL Aug 16 '13 at 10:50
  • 3
    You'd use pointers when, for example, you'd want to store instances of polymorphic classes all deriving from the same base. And even then, it's better to use smart pointers. Something like `vector> v;` – jrok Aug 16 '13 at 10:50
  • Why do people intend on using structures in C++? when classes are there.... right there...waving wanting-ly. Id personally use a pointer to an array of pointers, that is unless you are going to have an undefined amount of structs. – SD1990 Aug 16 '13 at 10:51
  • 2
    @SD1990 Are you aware that structs and classes are almost identical in C++? – jrok Aug 16 '13 at 10:52
  • I'm also afraid of memory leaks and how could I delete this vector correctly then, when using pointers... – skater_nex Aug 16 '13 at 10:54
  • @SD1990 Maybe he just wanted the convenience of not having to write `public` anywhere for such simple a type? Why do people intend on using classes instead of structs in C++, when both are almost identical? – Christian Rau Aug 16 '13 at 10:55
  • 1
    @SD1990, why classes when all I want is just store small two vars – skater_nex Aug 16 '13 at 10:56
  • @jrok yeh i am, apart from the whole public thing. Its just personal preference really, i mean i think id rather head toward a class because its C++ and its there to use, and leave the structs for programming in C. But you are right theres no difference – SD1990 Aug 16 '13 at 10:58
  • 2
    @skater_new Also you have //whatever in your code, which could mean anything after those two variables, not knowing what //whatever mean could mean you have variables that could be better suited to a class rather than a struct – SD1990 Aug 16 '13 at 11:00
  • Thanks guys for your help. Pointers make me nuts as a beginner :( – skater_nex Aug 16 '13 at 11:06

3 Answers3

4

Storing raw pointers

vector<myStruct*> v;

is a bad idea. Who is supposed to delete them? It is safer to use a smart pointer, e.g. in C++11

vector<std::shared_ptr<myStruct>> v;

or

vector<std::unique_ptr<myStruct>> v;

depending on what you are doing. See here

If you have a simple value type, it is easier to copy them i.e. as you suggest

vector<myStruct> v;

Otherwise, if you want OO polymorphism you would go for a pointer to base in your collection.

Community
  • 1
  • 1
doctorlove
  • 18,872
  • 2
  • 46
  • 62
  • Meh, might be a +1 if it wouldn't advocate the wrong smart pointer without a word about it and if it hadn't the definite best solution as the last one. But ok, it's definitely not a -1 at least. – Christian Rau Aug 16 '13 at 10:59
  • 1
    @ChristianRau I backed off from going into a big discussion and plumped for the stupid option. I've added a link ... – doctorlove Aug 16 '13 at 11:04
1

It depends if your vector will instantiate those structures or not. If former, you don't have any reason to store pointers at all. Pointers are helpful if you store actual values somewhere else, but need to gather pointers to those values in one place.

GuardianX
  • 515
  • 11
  • 29
1

Normally store values in STL container is the best practice way, then you don't need to worry about memory clean up etc.

 std::vector<myStruct> v;  //#1 this is GOOD

 std::vector<myStruct*> vp; //#2 this is BAD, you need to clean pointer elements by yourself

in case 2, you have to clean up dynamically allocated memory by yourself, something like:

std::vector<myStruct*> vp;
for(auto it = vp.begin(); it!= vp.end(); ++it)
{
     delete *it;   // release memory manually
                   // *it is the elemnt which is poiter, not iterator itself  
}

Just wander in what cases using of pointers should be ok. Thought that using pointers should minimize memory usage.

When you need to keep polymorphism, say myStruct serves as an interface purpose, you could store it in STL container as pointer.

#include <memory>
#include <vector>

struct myStruct 
{
  virtual ~myStruct() {};
};

struct Derived : public myStruct 
{
};

std::vector<std::unique_ptr<myStruct>> v;   

v.push_back(std::unique_ptr<myStruct>(new myStruct));  // pointer points to base object
v.push_back(std::unique_ptr<myStruct>(new Derived));   // pointer points to derived object
billz
  • 44,644
  • 9
  • 83
  • 100
  • so as I understand I literally cant clean pointer when using vector v, right? – skater_nex Aug 16 '13 at 11:01
  • 1
    that's not right. you need to loop through container and delete them one by one. – billz Aug 16 '13 at 11:02
  • okay i see now. so there is no way to delete them all at once. that's make sence to not using pointers in this case. thanks a lot. – skater_nex Aug 16 '13 at 11:03
  • 2
    @skater_nex note that your pointer could point to a stack allocated object that does not need to be deleted. You would need to enforce that all the vectors elements are dynamically allocated or not. – juanchopanza Aug 16 '13 at 11:05