Home > Enterprise >  Can we customize the implementation of STL containers?
Can we customize the implementation of STL containers?

Time:11-20

From The C programming language book by Bjarne Stroustrup:

The C   programming language| STL containers

Does using a rep pointer to element representation in a container imply that the implementation of the STL containers is flexible and can be customized?

CodePudding user response:

This diagram is for illustration purposes, only. It is meant to explain the underlying fundamental concepts of various containers, such as linked lists and maps.

None of the containers in the C library expose these kind of internal details. They expose only the documented methods, as explained in your C textbook and the C standard. std::list for example (which is a doubly-linked list), exposes begin() and end(), that return the beginning and the ending iterator; and several other methods that provide access to the contents of the list. None of these kinds of internal pointers get exposed.

The extent of "customization" is defined by the container itself, namely by the template parameters. The first template parameter to std::list defines what's in the linked list. That's your customization: it customized what's in the linked list, an ordinary int or a complicated class. Other parameters to other templates specify additional level of customizations. Various parameters to std::map, for example, specify the map's key and value, and a comparison operator that sets the order of the iteration of the map's key. That's quite a bit of customization, there.

CodePudding user response:

There is no "rep pointer," or at least none which is exposed to the user as such. That is merely an example of how the implementation might go about implementing a particular container.

And no, the internals of the standard library are not available for "customization". At least, not as defined by the standard. Different implementations could have #defines or something to allow you to pick some specifics, but those would be defined by particular implementations.

Broadly speaking, if you need to control specific details of a particular container, beyond what the standard library allows, you implement it yourself.

CodePudding user response:

Regardless of the meaning of "rep", yes you may customize. However, for STL classes, generally, no concept of a need for customization is implied. The classes are designed by experts to do their job well as is. (Also note, as Sam Varshavchik stated, that forward_list is templated and so can be used to manage various object types.)

This is the Microsoft STL source code for forward_list: https://github.com/microsoft/STL/blob/main/stl/inc/forward_list Search for "class forward_list" to see the top-level class. One can see the primitives such as nodeptr and iterator. These and other available classes can be used to construct any class that you would like, provided that one adheres to the license. https://github.com/microsoft/STL/blob/main/LICENSE.txt

CodePudding user response:

If you are a standard library implementer, then yes you are allowed to customize the containers any ways you wanted, as long your implementation can satisfy all the criteria published by the C standard.

For instance, while std::map if mostly implemented as a red-black tree, it is not required to do so.


As a user, no you are not exposed with the ability to customize the implementation of them.


Note, there are container adapters and ranges/views that only give you loose requirements on how the underlying containers should behave like, which you can use your own containers as well. But they aren't necessarily considered STL container, and I'm assuming that's not what you were asking for either.

  • Related