To achieve your goal, there may be an implementation-specific (non-portable) method, but not portable.
In the general case, std::map is implemented as a type of binary tree, usually sorted by key. The definition of the first element differs depending on the order. Also, in your definition, is the [0] node element at the top of the tree or the leftmost leaf of the node?
Many binary trees are implemented as linked lists. Most linked lists cannot be retrieved directly, like an array, because to find element 5 you need to follow the links. This is by definition.
You can solve your problem using both std::vector and std::map :
- Select an object from dynamic memory.
- Save the pointer with the key in
std::map . - Save the pointer to
std::vector in the right place in.
std::map will allow an efficient method of accessing an object by key.
std::vector will allow an efficient method of accessing an object by index. Storing pointers allows you to use only one instance of an object, rather than supporting multiple copies.
Thomas Matthews
source share