C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

string.fine(string+string...

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Gary Wessle
Guest





PostPosted: Thu Aug 17, 2006 1:29 am    Post subject: string.fine(string+string... Reply with quote



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
Back to top
Gary Wessle
Guest





PostPosted: Thu Aug 17, 2006 3:11 am    Post subject: Re: string.fine(string+string... Reply with quote



Gary Wessle <phddas (AT) yahoo (DOT) com> writes:

Quote:
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.
Back to top
Guest






PostPosted: Fri Aug 18, 2006 9:10 am    Post subject: Re: string.fine(string+string... Reply with quote



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
Back to top
Old Wolf
Guest





PostPosted: Sat Aug 19, 2006 9:10 am    Post subject: Re: string.fine(string+string... Reply with quote

Philip Potter wrote:
Quote:
"Gary Wessle" <phddas (AT) yahoo (DOT) com> wrote:

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();

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.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.