Reputation: 71
How can you split without making copies of string?
Upvotes: 4
Views: 736
Reputation: 299730
You could use a recipient akin to llvm::StringRef
, which is merely a pointer into a char
array and a size, and provides no mutator to the underlying sequence.
However that would mean recoding the split logic on yourself.
Upvotes: 0
Reputation: 48066
You could use std::regex
as defined in C++0x or in C++98 TR1 - this returns iterators into the string (well, behind a facade anyhow) - so it doesn't involve copying the string. The C++0x regex variant supports both extracting matches and splitting (extracting non-matches) - so it's a full replacement for strtok
with lots of additional power.
See John Cook's webpage for example, wikipedia or a video by Stephan T Lavavej. You may need to use boost::regex until C++0x is more widely implemented; the two are compatible.
Upvotes: 1
Reputation: 490048
Using Boost Split, you can't. The obvious (but ugly) way to split strings without copying them would be strtok
(or, preferably, strtok_s
).
Upvotes: 0