search and replace

R

Richard Herring

Neil Cerutti said:
Well, it's hard to think of anything better.


It looks slower to me, as at least two copies of the input string
have to be made, whereas the string member function version
doesn't necessarily make any copies. Moreover, iostreams are
heavy duty iron for such a simple task.

I'd call the idea clever but impractical. I'm not sure if it's
smarmy. ;-)
For smarmy++, there should be no explicit use of for, while, if, etc.,
which should all be hidden within an appropriate STL algorithm. Some of
the other competitors don't seem to have absorbed this principle ;-)
 
N

n2xssvv g02gfr12930

Phlip said:
C++ers:

Here's an open ended STL question. What's the smarmiest most templated way
to use <string>, <algorithms> etc. to turn this:

" able search baker search charlie "

into this:

" able replace baker replace charlie "

Note that "replace" has one more letter than "search", so you can't do an
inplace substitution.

An alternate prize goes to whoever provides an efficient way (that doesn't
use simple things like character pointers and buffers and C things ;).

(We are aware several C++ Regexps are available; we just need to learn more
raw STL here.)

Using routines to find the substring ,to reverse the end of the string
and add a string to the end will do the job. The sequence of
intermediate strings would be as follows:

" able search baker search charlie "
" able ielrahc hcraes rekab hcraes"
" able ielrahc hcraes rekab "
" able ielrahc hcraes rekab ecalper"
" able replace baker search charlie "
" able replace baker ielrahc hcraes"
" able replace baker ielrahc "
" able replace baker ielrahc ecalper"
" able replace baker replace charlie "

You should be able to work the rest out for yourself. But as you can see
this method only requires the buffer for the string to be increased by 2
characters and no separate buffers, as well as using simple, easily
understood algorithms.

JFJB
 
N

n2xssvv g02gfr12930

n2xssvv said:
Using routines to find the substring ,to reverse the end of the string
and add a string to the end will do the job. The sequence of
intermediate strings would be as follows:

" able search baker search charlie "
" able ielrahc hcraes rekab hcraes"
" able ielrahc hcraes rekab "
" able ielrahc hcraes rekab ecalper"
" able replace baker search charlie "
" able replace baker ielrahc hcraes"
" able replace baker ielrahc "
" able replace baker ielrahc ecalper"
" able replace baker replace charlie "

You should be able to work the rest out for yourself. But as you can see
this method only requires the buffer for the string to be increased by 2
characters and no separate buffers, as well as using simple, easily
understood algorithms.

JFJB
Here's the code using STL library functions

void ReplaceTest(void)
{
std::string Tmp(" able search baker search charlie ");

std::cout << Tmp << std::endl;

std::string::size_type p = Tmp.find("search",0);
std::reverse(Tmp.begin() + p,Tmp.end());
Tmp.erase(Tmp.end() - 6,Tmp.end());
Tmp += "ecalper";
std::reverse(Tmp.begin() + p,Tmp.end());

p = Tmp.find("search",0);
std::reverse(Tmp.begin() + p,Tmp.end());
Tmp.erase(Tmp.end() - 6,Tmp.end());
Tmp += "ecalper";
std::reverse(Tmp.begin() + p,Tmp.end());

std::cout << Tmp << std::endl;
}

JFJB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top