4

How can you split without making copies of string?

0

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Using Boost Split, you can't. The obvious (but ugly) way to split strings without copying them would be strtok (or, preferably, strtok_s).

2 Comments

I thought you could do with iterator_range type using boost.
@JerryCoffin Do you know why Boost split takes a reference and not a const reference for the Input argument? I would assume that the split function would never change the input.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.