4

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.

leventov
  • 14,760
  • 11
  • 69
  • 98

1 Answers1

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