By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,301 Members | 1,378 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,301 IT Pros & Developers. It's quick & easy.

Substitution in the C++ string class

P: n/a
I was extremely surprised to learn that the extremely rich C++ string
API does not have even a single menthod devoted to string substitution
i.e. given a string, replace all instances of pattern-1 in the string
with pattern-2. There are API methods for finding and replacing, but
none on pattern substitution.

Although I have developed an implementation for this (posted to the
comp.sources.d newsgroup), does anyone have the background why it was
not deemed necessary to provide this functionality in the standard C+
+ string API in the first place?

Thanks,
Song

Jun 9 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Generic Usenet Account wrote:
I was extremely surprised to learn that the extremely rich C++ string
API does not have even a single menthod devoted to string substitution
i.e. given a string, replace all instances of pattern-1 in the string
with pattern-2. There are API methods for finding and replacing, but
none on pattern substitution.
Have you looked at std::tr1::regex (from boost)?
Although I have developed an implementation for this (posted to the
comp.sources.d newsgroup), does anyone have the background why it was
not deemed necessary to provide this functionality in the standard C+
+ string API in the first place?
There were many things not included in the standard library, so I guess
regular expressions were one of them!

--
Ian Collins.
Jun 9 '07 #2

P: n/a

"Generic Usenet Account" <us****@sta.samsung.comwrote in message
news:11*********************@q66g2000hsg.googlegro ups.com...
I was extremely surprised to learn that the extremely rich C++ string
API does not have even a single menthod devoted to string substitution
i.e. given a string, replace all instances of pattern-1 in the string
with pattern-2. There are API methods for finding and replacing, but
none on pattern substitution.
I felt the same, and I also had to impliement "Substitute()", using
a non-std regex engine from djgpp. I've always thought this was one
of the biggest and most egregious ommissions from the std. lib.
Although I have developed an implementation for this (posted to the
comp.sources.d newsgroup), does anyone have the background why it was
not deemed necessary to provide this functionality in the standard C+
+ string API in the first place?
(Shrugs.) Perhaps the std. committee felt it was a feature better left
to libraries other than the standard library.

--
Cheers,
Robbie Hatley
lone wolf aatt well dott com
triple-dubya dott Tustin Free Zone dott org
Jun 9 '07 #3

P: n/a
On Fri, 08 Jun 2007 18:04:33 -0700, Generic Usenet Account wrote:
>I was extremely surprised to learn that the extremely rich C++ string
API does not have even a single menthod devoted to string substitution
Although I have developed an implementation for this (posted to the
comp.sources.d newsgroup), does anyone have the background why it was
not deemed necessary to provide this functionality in the standard C+
+ string API in the first place?
Not many developers are really satisfied with the 'C++ string'. At
first sight your implementation is a little intricate, esp. 'status'.
IMO, it should also be changed so that at most one dynamic allocation
is performed within the function. Otherwise it may be inefficient for
longer strings when the new token is longer than the replaced.
Moreover, I don't see why you implement the function as operator().
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jun 9 '07 #4

P: n/a
On Jun 9, 3:04 am, Generic Usenet Account <use...@sta.samsung.com>
wrote:
I was extremely surprised to learn that the extremely rich C++ string
API does not have even a single menthod devoted to string substitution
i.e. given a string, replace all instances of pattern-1 in the string
with pattern-2. There are API methods for finding and replacing, but
none on pattern substitution.
Although I have developed an implementation for this (posted to the
comp.sources.d newsgroup), does anyone have the background why it was
not deemed necessary to provide this functionality in the standard C+
+ string API in the first place?
Probably because it doesn't belong there. (Of course, there are
some things that are there that don't belong there, like all of
the find functions, etc.)

For the most part, the philosophy behind the standard library is
that the containers contain, and define sequences, and that
there are separate algorithms which work on sequences. Thus, I
can use std::replace (from <algorithm>) on a string, but also on
a vector, a deque or a list. The same thing holds for regular
expressions, which have been added to the standard; I can do a
regular expression search and replace on a vector<int>, for
example, if that's what I need.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 9 '07 #5

P: n/a
On Sat, 09 Jun 2007 19:51:47 -0000, James Kanze wrote:
>Thus, I
can use std::replace (from <algorithm>) on a string, but also on
a vector, a deque or a list.
OTOH, what basic_string::replace() does (or the desired replace_all())
cannot be done with std::replace().
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jun 10 '07 #6

P: n/a
On Jun 10, 12:01 pm, rpbg...@yahoo.com (Roland Pibinger) wrote:
On Sat, 09 Jun 2007 19:51:47 -0000, James Kanze wrote:
Thus, I
can use std::replace (from <algorithm>) on a string, but also on
a vector, a deque or a list.
OTOH, what basic_string::replace() does (or the desired replace_all())
cannot be done with std::replace().
Yes. There are a very few fundamental operations on string
which are just for string. Replace and its derivatives (insert,
append, erase) are examples. But note that despite having the
same name, std::replace and std::basic_string::replace have two
very different semantics: std::replace (or std::replace_if)
replaces according to the value; std::basic_string::replace
replaces according to position.

In other containers, you don't have replace, but you do have
insert and erase; operations which modify the topology of the
container are generally members.

The original question concerned something somewhat more complex,
since it involved a replace operation changing topology, but
dependent on value, and not position. As such, it certainly
doesn't fit as a member (because of value), and can't be done
with the classical algorithms, because they generally don't
support changing topology. Thus, a totally new component,
regex.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 10 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.