Click to See Complete Forum and Search --> : Searching a string


VB-pretty-n-da-best
May 14th, 2005, 01:56 AM
I'd like to search for a short string in a long string
but find() can only give me the position of the first meet, whereas I want to take out all of their matching positions. How can i do this in C and or or C++ ?
Thank you

Eample: input: There is a skeleton in his cupboard and a small gun in his shirt's pocket
search for in, and return two matches

Yves M
May 14th, 2005, 02:33 AM
Just use std::find in a loop.


std::vector<std::string::size_type> find_all(const std::string &s, const std::string &tofind)
{
std::vector<std::string::size_type> result;
std::string::size_type pos;
pos = s.find(tofind);
while (pos != std::string::npos) {
result.push_back(pos);
pos = s.find(tofind, pos + 1);
}
return result;
}

This constructs a vector of all the positions where the substring occurs.

VB-pretty-n-da-best
May 15th, 2005, 02:09 AM
Thank you yves help me.
Could yves tell what is differnt between
do {
/////////

}while(condition)

and
while(condition){
////////
}

which one more effective ? if in thoes blocks have lot of codes, or are they the same ?
my teacher says they are the same.
Thank you

Smasher/Devourer
May 15th, 2005, 02:31 AM
The difference between the two loops you posted is that in a while loop, the loop condition is checked before the body of the loop is executed. Thus, when the while statement is first encountered, if the condition is false, the loop will not run at all. In the do...while loop, on the other hand, the condition is checked after the body of the loop executes, so the loop will always run at least once. For example:
int n = -1;
while (n > 0)
{
// loop code here
}
In the above code snippet, the lines inside the loop will never be executed. Compare that to:
int n = -1;
do
{
// loop code here
} while (n > 0);
Here the body of the loop will run exactly once, because the program doesn't check whether n > 0 until after the body of loop has run.

There's no difference in performance between the two; which one you use will simply depend on the nature of your problem in each situation. If the possibility exists that your loop should not run at all, use the while loop. If you want the loop to run at least once no matter what happens, the do...while loop may be more appropriate.

VB-pretty-n-da-best
May 15th, 2005, 02:41 AM
I asked beacause i set a breakpoint and debug and see things differnt. thank you,

btw, i see yves online but yves didn't anser, i guess he ansers original question only, nevr for my second question. :D again, thanks

grabbler
May 15th, 2005, 11:47 AM
You saw it different because of your pieces of code constructs you have created.
Smashers/Devourer explanations was correct about how you could obtain the results output from the two "while's" but I think another point that should be taken into consideration is about the optimization problem of code structure in which the pretested loop does not give the best performance or the speed to run a bunch of code if present in the 'while' block. This is because there needs to be an extra unconditional jump at the end of the loop block to return to the beginning of the loop whereas the posttested loops in fact needs no such a jump and they become more effecient.
I guess so...

exterminator
May 16th, 2005, 08:03 AM
This is because there needs to be an extra unconditional jump at the end of the loop block to return to the beginning of the loop whereas the posttested loops in fact needs no such a jump and they become more effecient.
I guess so...

I don't think so ! I don't understand the mechanism. And I feel even though this happens to be the case it would be there in both the cases.

I feel the only difference here is what has been already pointed out by Smasher/Devourer.

Cheers,
Exterminator