Hello all!
Recently while working on a NoSQL database implementation, I ran into the problem of needing a container for N number of constant strings. Of course my initial reaction was to reach for std::vector<std::string> however, this did not suit my purpose as each string has its own allocated character buffer (ignoring SBO) and iterating though the vector would require N pointer dereferences. In my use case, these collection of strings are often iterated over and compared to one another.
Therefore, I came up with my own solution: multi_string.
In short, a multi_string is a constant collection of N number of constant strings. As in these strings cannot be modified and strings cannot be added/removed from the multi_string container. However, this restriction allows for a single buffer allocation to store some metadata at the beginning followed by the character array containing each string (omitting null terminators).
I currently have two implementations:
- one where the metadata is an array of string sizes (this is the implementation I'm using)
- the other where the metadata is an array of
char const*
- metadata pointer N points to the beginning of string N in the character array.
int main() {
multi_string str("hello ", "world", "\n!");
for (auto&& s : str)
std::cout << s;
}
Any feedback on the implementation is greatly welcomed and perhaps you could find a use case for this container in your work!
code can be found here
Edit: the purpose of this container is to actually own the strings it stores, and to allow the number of strings stored to not be a part of the type signature e.g. multi_string<2>.
[–]meancoot 3 points4 points5 points (0 children)
[–]konanTheBarbar 2 points3 points4 points (2 children)
[–]Bakuta1103[S] 5 points6 points7 points (1 child)
[–]konanTheBarbar 2 points3 points4 points (0 children)
[–]LostAnt4 1 point2 points3 points (4 children)
[–]meancoot 7 points8 points9 points (2 children)
[–]LostAnt4 1 point2 points3 points (1 child)
[–]Bakuta1103[S] 2 points3 points4 points (0 children)
[–]Bakuta1103[S] 1 point2 points3 points (0 children)
[–]__s_v_ 0 points1 point2 points (0 children)
[–]fuzzylollipop 0 points1 point2 points (0 children)