 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gary Wessle Guest
|
Posted: Thu Aug 17, 2006 1:29 am Post subject: string.fine(string+string... |
|
|
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
|
Posted: Thu Aug 17, 2006 3:11 am Post subject: Re: string.fine(string+string... |
|
|
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
|
Posted: Fri Aug 18, 2006 9:10 am Post subject: Re: string.fine(string+string... |
|
|
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
|
Posted: Sat Aug 19, 2006 9:10 am Post subject: Re: string.fine(string+string... |
|
|
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 |
|
 |
|
|
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
|
|