473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

std::string question

Hello,

I would like to replace all the occurrence of "(" and ")"
in C++ std::string to "\(" and "\)".

For example:
string s = "(abc)|(toto)|(lala)"
will be become "\(abc\)|\(toto\)|\(lala\)"
Question:
Just wonder if there is a straight forward function to do so??
Currently I am using a while loop to read through the string, then
basic_string::find and basic_string::replace the occurrence of "(" and ")".

Any better solution?

Thanks,
Kian
Jul 22 '05 #1
6 2592

"Kian Goh" <ki**@hotmail.com> wrote in message
news:g_********************@rogers.com...
Hello,

I would like to replace all the occurrence of "(" and ")"
in C++ std::string to "\(" and "\)".

For example:
string s = "(abc)|(toto)|(lala)"
will be become "\(abc\)|\(toto\)|\(lala\)"
Question:
Just wonder if there is a straight forward function to do so??
No.


Currently I am using a while loop to read through the string, then
basic_string::find and basic_string::replace the occurrence of "(" and
")".

Any better solution?


Certainly there are bad ways and good ways to write this code but without
seeing the code you have written it is impossible to say whether there is a
better solution.

A good solution would work in three phases.

1) Go though the string seeing how many backslashes you are going to need to
insert
2) Increase the size of the string by the number of extra backslashes needed
3) Loop backwards though the string, copying characters and inserting
backslashes as necessary.

The point of this compexity is that it minimises reallocation of the string
and copying of characters.

A bad solution would work like this

1) Go forwards through the string and each time you find a "(" or ")" insert
a backslash.

john
Jul 22 '05 #2
"Kian Goh" <ki**@hotmail.com> wrote in message
news:g_********************@rogers.com...
Hello,

I would like to replace all the occurrence of "(" and ")"
in C++ std::string to "\(" and "\)".

For example:
string s = "(abc)|(toto)|(lala)"
will be become "\(abc\)|\(toto\)|\(lala\)"
Question:
Just wonder if there is a straight forward function to do so??
Currently I am using a while loop to read through the string, then
basic_string::find and basic_string::replace the occurrence of "(" and
")".

Any better solution?

Thanks,
Kian


Here is a solution:

string add_backslash_to_brackets(char ch)
{
string s(1, ch);
if (ch == '(' || ch == ')')
s.insert(s.begin(), '\\');
return s;
}

vector<string> v;
transform(s.begin(), s.end(), back_inserter(v), add_backslash_to_brackets);
s = accumulate(v.begin(), v.end(), string());

Hope you like it.
Jul 22 '05 #3
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:419b0f0b$0$24380
"Kian Goh" <ki**@hotmail.com> wrote in message
For example:
string s = "(abc)|(toto)|(lala)"
will be become "\(abc\)|\(toto\)|\(lala\)"

string add_backslash_to_brackets(char ch)
{
string s(1, ch);
if (ch == '(' || ch == ')')
s.insert(s.begin(), '\\');
return s;
}

vector<string> v;
transform(s.begin(), s.end(), back_inserter(v), add_backslash_to_brackets); s = accumulate(v.begin(), v.end(), string());


Good.

Along the same lines, the function back_inserter(v) returns an iterator,
specifically a std::back_insert_iterator<std::vector<std::string> >, whose
operator= calls v.push_back(...). You could write your own
back_insert_iterator class too, thus avoiding the need to create strings and
a vector of strings.

class special_back_insert_iterator : public
std::back_insert_iterator<std::string> {
typedef std::back_insert_iterator<std::string> inherited;
public:
special_back_insert_iterator(std::string& s) : inherited(s) { }
special_back_insert_iterator& operator=(char c) {
if (ch == '(' || ch == ')') inherited::operator=('\\');
inherited::operator==(c);
return *this;
}
};

inline
special_back_insert_iterator special_back_inserter(std::string&)
{
return ...;
}

string s2;
transform(s.begin(), s.end(), special_back_inserter(s2));
Jul 22 '05 #4
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:nx*******************@bgtnsc05-news.ops.worldnet.att.net...
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:419b0f0b$0$24380
"Kian Goh" <ki**@hotmail.com> wrote in message

> For example:
> string s = "(abc)|(toto)|(lala)"
> will be become "\(abc\)|\(toto\)|\(lala\)"

string add_backslash_to_brackets(char ch)
{
string s(1, ch);
if (ch == '(' || ch == ')')
s.insert(s.begin(), '\\');
return s;
}

vector<string> v;
transform(s.begin(), s.end(), back_inserter(v),

add_backslash_to_brackets);
s = accumulate(v.begin(), v.end(), string());


Good.

Along the same lines, the function back_inserter(v) returns an iterator,
specifically a std::back_insert_iterator<std::vector<std::string> >, whose
operator= calls v.push_back(...). You could write your own
back_insert_iterator class too, thus avoiding the need to create strings
and
a vector of strings.

class special_back_insert_iterator : public
std::back_insert_iterator<std::string> {
typedef std::back_insert_iterator<std::string> inherited;
public:
special_back_insert_iterator(std::string& s) : inherited(s) { }
special_back_insert_iterator& operator=(char c) {
if (ch == '(' || ch == ')') inherited::operator=('\\');
inherited::operator==(c);
return *this;
}
};

inline
special_back_insert_iterator special_back_inserter(std::string&)
{
return ...;
}

string s2;
transform(s.begin(), s.end(), special_back_inserter(s2));


Nice. It all happens in one line of STL. I wonder how these STL solutions
compare with non-STL ones in terms of code complexity. Alot better I
presume.
Jul 22 '05 #5
On Tue, 16 Nov 2004 21:59:27 -0500 in comp.lang.c++, "Kian Goh"
<ki**@hotmail.com> wrote,
I would like to replace all the occurrence of "(" and ")"
in C++ std::string to "\(" and "\)".

#include <string>
#include <algorithm>
#include <numeric>
#include <iostream>

std::string changer(std::string was, char in)
{
if (in == '(')
return was+"\\(";
if (in == ')')
return was+"\\)";
return was+in;
};

int main()
{
std::string foo("(abc)|(toto)|(lala)");
std::string bar = std::accumulate(foo.begin(), foo.end(),
std::string(""), changer);
std::cout << bar;
}

Jul 22 '05 #6
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:419b27d6$0$27445
"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message

string s2;
transform(s.begin(), s.end(), special_back_inserter(s2));


Nice. It all happens in one line of STL. I wonder how these STL solutions
compare with non-STL ones in terms of code complexity. Alot better I
presume.


Except for the meaningless name 'special_back_inserter', the code above
looks simpler. However, it's not an inplace algorithm, therefore uses more
memory. John's suggestion is in in-place algorithm, though harder to
implement.
Jul 22 '05 #7

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

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
12
by: jl_post | last post by:
Dear C++ community, I have a question regarding the size of C++ std::strings. Basically, I compiled the following code under two different compilers: std::string someString = "Hello, world!";...
2
by: zhege | last post by:
I am a beginner of C++; I have a question about the std:string and std:cout class; Two pieces of code: -------------------------------- #include <iostream> #include <string> using namespace...
1
by: zhege | last post by:
I am a beginner of C++; I have a question about the std:string and std:cout class; Two pieces of code: -------------------------------- #include <iostream> #include <string> using namespace...
8
by: vidya.bhagwath | last post by:
Hello Experts, I am using std::string object as a member variable in one of the my class. The same class member function operates on the std::string object and it appends some string to that...
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
25
by: Bala2508 | last post by:
Hi, I have a C++ application that extensively uses std::string and std::ostringstream in somewhat similar manner as below std::string msgHeader; msgHeader = "<"; msgHeader += a; msgHeader...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.