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 6 2589
"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
"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.
"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));
"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.
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;
}
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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(...
|
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...
|
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
|
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!";...
|
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...
|
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...
|
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...
|
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;
};
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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...
| |