4 ms·
A nice follow up would also be looking at std::string_view as this has the potential to save a lot of memory.
by gravypod 5y ago
A nice follow up would also be looking at std::string_view as this has the potential to save a lot of memory.
- eatonphil 5y agoSince I'm unfamiliar with the underlying concept, could you give an example of how you could see that being used?
- gravypod 5y ago`std::string_view` is essentially something like this: struct string_view { char *data; size_t length; } It is a pointer to a subset of an existing string. It has all of the existing features of `std::string` but passing it around is zero copy. As long as the original memory allocation exists your `string_view`s are still valid. Also, it has an implicit constructor from `const std::string` and `const char #` allowing you you define a single function like `Thing ParseThing(std::string_view line)` and accept `char #` and `std::string` as an input. More examples are here: https://abseil.io/tips/1 https://abseil.io/tips/1 # -> *. HN formats bad
- Arnavion 5y ago\* (backslash asterisk) will escape the asterisk. >Also, it has an implicit constructor from `const std::string` and `const char *` allowing you you define a single function like `Thing ParseThing(std::string_view line)` and accept `char *` and `std::string` as an input.
- jcelerier 5y agoevery function where you are reading from a string without writing to it or saving it, you can use std::string_view. Same for vector / array: anything non-owning, read-only should use std::span instead.