string.fine(string+string...

G

Gary Wessle

Hi

after reading the docs, I was hoping this would work, but I can't find
out why.

string a = "I ";
string b = "am";
int ego;
if(sentence.find((a+b).c_str(),0)!= string::npos) ego++;

from the docs
****************
size_type find( const string& str, size_type index );
size_type find( const char* str, size_type index );
size_type find( const char* str, size_type index, size_type length );
size_type find( char ch, size_type index );

thanks
 
G

Gary Wessle

Gary Wessle said:
Hi

after reading the docs, I was hoping this would work, but I can't find
out why.

string a = "I ";
string b = "am";
int ego;
if(sentence.find((a+b).c_str(),0)!= string::npos) ego++;

from the docs
****************
size_type find( const string& str, size_type index );
size_type find( const char* str, size_type index );
size_type find( const char* str, size_type index, size_type length );
size_type find( char ch, size_type index );

thanks

the fix
if(sentence.find(a + b,0)!= string::npos) ego++;

I don't know why this worked, I tried "before posting"

if(sentence.find(a+b,0)!= string::npos) ego++; for no avail.

it seams then that a + b is not the same as a+b in this case.
 
T

tolgaceylanus

It works for me. Can you post the complete code (including what
"sentence" is)? And ego's
scope is not clear, you might need initializing it if it's in local
scope.

BTW, you should be able to call find using;

sentence.find(a+b)

you can skip the 0 index and converstion to C-string.

Tolga Ceylan
 
T

Thomas J. Gritzan

Gary said:
the fix
if(sentence.find(a + b,0)!= string::npos) ego++;

I don't know why this worked, I tried "before posting"

if(sentence.find(a+b,0)!= string::npos) ego++; for no avail.

it seams then that a + b is not the same as a+b in this case.

It is the same. Write complete and compileable code and post the error
message from your compiler.
 
P

Philip Potter

Gary Wessle said:
Hi

after reading the docs, I was hoping this would work, but I can't find
out why.

....you can't find out why you were hoping it would work?
....you can't find out why it did work?
What do you mean?
string a = "I ";
string b = "am";
int ego;
if(sentence.find((a+b).c_str(),0)!= string::npos) ego++;

The call to c_str() there will cause you headaches. (a+b) creates a
temporary string, which will be destroyed when it is no longer needed. This
can be after the call to c_str() but before the call to find(); in which
case, the pointer returned by c_str() will point to memory which has since
been deallocated. This is a bad thing.

One way to solve this problem is to make an explicit temporary:

string tmp = a + b;
if(sentence.find(tmp.c_str(),0)!=string::npos) ego++;

this works because tmp lasts until the end of the current scope.

A better solution, which others have posted elsethread, is to leave out the
call to c_str() entirely.

Philip
 
O

Old Wolf

Philip said:
The call to c_str() there will cause you headaches. (a+b) creates a
temporary string, which will be destroyed when it is no longer needed. This
can be after the call to c_str() but before the call to find();

Actually temporary objects last until the end of the full-expression
in which they were created -- in this case, until the semi-colon.

The code should work, although it is unnecessarily bloated:
sentence.find( a + b )
is sufficient.
 

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,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top