What is the correct container to manage small objects that map to some other small objects?

I have a collection of small objects. Each of these objects points to other objects. These pointers can be implemented as actual pointers or indices to an array of objects or whatever. It can be an array of such pointers, the length of which can change. There can be pointers to objects of one type and to other types, but this is known at compile time.

So for example: I have a Person class. This person has two pointers to his parent Faces. There is another cool place there. Each person has a list of pointers to all the places they have visited.

Unlike the actual family tree, I can change the tree from time to time by deleting / inserting some people.

Is there a container in the C ++ Standard Library (C ++ 11) for this purpose, or would I be better off looking for a dedicated memory management class?

I need to pass data to a C interface, which is why I prefer a storage method based on an accessible (read-only) linear array.

+3


source to share


3 answers


Questions:

  • "There may be pointers to objects of one type and to other types, but this is known at compile time."
  • "These pointers can be implemented as actual pointers or indices to an array of objects or whatever."
  • Unlike the actual family tree, I can change the tree from time to time by deleting / inserting some Faces.
  • "Is there a container in the C ++ Standard Library (C ++ 11) for this purpose, or should I look for a dedicated memory management class?"
  • "I need to pass data to a C interface, which is why I prefer a read-only linear array based storage method."

Answers:



  • This can get tricky if you want to use pointers to multiple object types in the same container. But you can do either a raw array or std::vector

    .
  • If you are just using indexes that will greatly simplify the problem with "pointers to multiple types". Again, either the raw array works, or std::vector

    .
  • Dynamically resizing an array of arrays becomes sketchy, I would say that is the point for std::vector

    .
  • Depending on the time you are willing to contribute, managing your memory is a huge job, I would stick with a raw array or std::vector

    until they lost my cold dead fingers.
  • If you have C ++ 11 with help std::vector::data

    , you can make it std::vector

    just as useful as a raw array here, if it won't be easier to implement as a raw array.

std::vector

has many benefits for you as a programmer. Not the least of which is automatic memory management. So I'm leaning towards std::vector

where is the option.

+2


source


Seems like a great time to quote Stepanov :

Use vectors whenever you can. If you can't use vectors, rework your solution to use vectors.



The "available line array" part indicates vector<Person>

- none of the other containers have this functionality, and the rest of your use case does not assume any specific storage or access. Part of it sounds like it might be cleaner if you can do it vector<shared_ptr<Person>>

, but that would violate your C interface requirement. So that vector<Person>

's probably your answer.

+5


source


I prefer to use a vector rather than another (list, etc.). as Bjarne said, "If you know int and vector, then you know C ++." Here is a Youtube link as Bjarne said about vector: Bjarne Stroustrup: Why You Should Avoid Linked Lists .

+1


source







All Articles