473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Double characters

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
Aug 25 '06 #1
1 1133
Mike C# wrote:
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
Aug 25 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: J | last post by:
Would anyone know if there a type tag to format a double? I have f for floating point, but cannot find one for double.
3
by: tony wong | last post by:
is it possible to detect any double-byte character in the text? thanks. tony
24
by: deko | last post by:
I'm trying to log error messages and sometimes (no telling when or where) the message contains a string with double quotes. Is there a way get the query to insert the string with the double...
1
by: Ed S | last post by:
I'm trying to format a double with this behavior: 1) should only show 8 characters (character count before and after decimal added together). 2) if greater than 7 characters to the left of the...
7
by: gar | last post by:
Hi, I need to replace all the double quotes (") in a textbox with single quotes ('). I used this code text= Replace(text, """", "'" This works fine (for normal double quotes).The problem...
16
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array...
3
by: Little | last post by:
Could someone help me get started on this program or where to look to get information, I am not sure how to put things together. 1. Create 4 double linked lists as follows: (a) A double linked...
1
by: Little | last post by:
Could someone help me figure out how to put my project together. I can't get my mind wrapped around the creation of the 4 double Linked Lists. Thank your for your insight. 1. Create 4 double...
1
by: Little | last post by:
Hello everyone. I am trying to do the following program and am unable to get the beginning portion to work correctly. The scanner works when I print the statements without the double linked list...
6
by: Jay | last post by:
I need to convert from a string a double that is followed by a scaling character (k means *1e3, M=*1e6, etc) then apply the scaling character. Example: "-1.345k #comment" I know roughly how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.