Double characters

  • Thread starter Thread starter Mike C#
  • Start date Start date
M

Mike C#

Hi all,

I'm using std::strings in a program, and was wondering what the most
efficient method of performing the following two tasks would be:

1. Locate all apostrophes (') in a string and replace with two apostrophes
('')
2. Locate all side-by-side double quotation marks ("") and replace with a
single quotation mark (")

Right now I'm looping through the individual characters of the string,
comparing them and appending the individual chars to another std::string.
There has to be a more efficient method.

Thanks
 
Mike said:
Right now I'm looping through the individual characters of the string,
comparing them and appending the individual chars to another std::string.
There has to be a more efficient method.

What you're doing is correct. At one moment you have to compare every
character, but you can probably do something to predict the output's length.

Every time you append a new character and the string's buffer happens to
be too small, a new memory block is allocated and the old string is
copied over, which is quite a bit of overhead. This could happen a
couple of times per string. You can prevent (or at least reduce) this by
reserving some space for the output.

You can start experimenting with

output.reserve(input.size());
// now you can append in a loop

If most of your strings have single quotes, you can try to increase the
reserved space. In the worst case, your output string will be twice as
large as your input, so if you don't mind wasting some memory, you can try

output.reserve(input.size() * 2);

It's not that much memory wasted, as std::string itself can use a buffer
capacity up to twice as large as the length of the string, when you keep
appending in a loop. This won't make the situation much worse.

Note that reserve itself doesn't change the actual length of your
string; it only changes the buffer's capacity. So don't try to access
output[n] after a reserve; just keep appending like before.

Sometimes you can beat the STL by hand-coding a C-style (char* based)
implementation, but I'd only worry if I noticed that even after using
reserve my strings represented a real bottleneck in the application.

Tom
 
Back
Top