I'm dealing with giant (up to 2GB) strings and their slices in C++ program. C-style strings seem to be unreliable under such circumstances, but can be sliced trivially (without '\0' at the end). On the other hand, as I understood, std::string::substr copies the slice, therefore I should perform at least one extra addition operation (index + base) per indexing in order to keep memory usage rational.
Asked
Active
Viewed 752 times
4
-
So far, this is not a real question. Besides this, can't you do (reading) slicing using iterators? – moooeeeep May 09 '12 at 09:48
-
I'm searching for a way to slice std::string effectively or confirmation that it is unreal. – leventov May 09 '12 at 09:51
-
Unreal to slice effectively, i meant. – leventov May 09 '12 at 10:08
1 Answers
8
The most general solution would be to create a slice object, with the interface you need, and use that. The slice object could consist of two iterators, the start and the end.

James Kanze
- 150,581
- 18
- 184
- 329
-
And decide what semantics you want for the slices: are the characters in the slice modifiable? If so is modifying the slice supposed to modify the full string? If modifiable but not shared, you need copy-on-write, which is an extra tonne of fun unless you can dig out some old COW implementation of `std::string` and use that (renamed). – Steve Jessop May 09 '12 at 10:00