You certainly could write a vector-like class that had a "small object optimization", where if the objects were small and few it would store them internally (inside the container object itself), and otherwise store them on the heap (like std::vector
does now).
However, this is not currently possible for the containers in the standard library, because of the requirements placed on them. In particular, [container.requirements.general]/9 says:
Every iterator referring to an element in one container before the swap shall refer to the same element in the other container after the swap.
That's hard to do with the small-object optimization, and that requirement does not apply to basic_string
.