|
ok, i get what's your problem then... you are trying to speak english with french words
don't say equalize but assign.
anyway, as you're using std::string s, just use the = operator for this, and == operator to compare their content.
std::string s1 = "Hello";
std::string s2 = "World";
ASSERT(s1 != s2);
s1 = s2;
ASSERT(s1 == s2);
|
|
|
|
|
OK, just to see if i have caught what you said:
I should use == to compare if string s1 has the same content with string s2.
That´s it?
Because I already have tried that and it doesn´t work...
|
|
|
|
|
Hum ... i did a mistake while typing the code, i always typed: if(s1==s2) but the thing i wanted to check is if (s1!=s2)
I am so sorry for the loss of time....
Anyway, thank you very much for your help!!!
|
|
|
|
|
garfaoui wrote: I should use == to compare if string s1 has the same content with string s2.
That´s it?
exactly. but be careful, the operator must be the overload of std::string class.
in the expression s1 == s2 , if s1 is of type std::string, the code actually means to the compiler s1.operator==(s2) (calling then the function bool std::string::operator==(const std::string&) ).
but if s1 is a basic C-Style string (a char* basically), the compiler will try to compare the addresses, not the string contents...
the context is exactly the same while using the operator != .
so, can you please post a relevant (but small) piece of code which can reproduce the error ?
also, specify to us the type of all the variables used, and which error you get (if it is a compiler error, linker error, or run-time error), and its message.
|
|
|
|
|
Francais et en stage au Portugal ?
|
|
|
|
|
tout a fait =)
|
|
|
|
|
Te laisse pas impressioner par Tox, il est dur, mais très très fort.
|
|
|
|
|
Ne t´inquète pas!
En tout cas il m´a bien aidé!
C´est par rapport au repertoire de l´executable que tu as vu que j´étais au Portugal?
|
|
|
|
|
non, par rapport a ton profil ^^
@Régis, merci pr la petite note d'humour... "il est tres tres fort", c'est a double tranchant ca
|
|
|
|
|
|
Nope, c'est dans ta biographie (clique sur l'icone en forme de tete a cote de ton nom)
|
|
|
|
|
Shilpi Boosar wrote: if(strcmp(s1,s2)==0)
lead to the following error: <;146 C:\Documents and Settings\garfaoui\Ambiente de trabalho\test\clippr\main.cpp no matching function for call to `strcmp(std::string&, std::string&)' >
|
|
|
|
|
string s1 = "shilpi";
string s2 = "shilpi";
if(strcmp(s1.c_str(),s2.c_str())==0)
{
printf("Hi");
}
what i wanna say is this ,try it
Yes U Can ...If U Can ,Dream it , U can do it ...ICAN
|
|
|
|
|
Hello everyone,
I find to initialize two dimentional array in Visual Studio, I have to specify the number of elements. For example,
<br />
const char foo[][] = {"hello", "world"};
const char goo[][64] = {"hello", "world"};
So, the best solution is to specify the number of elements of the 2nd dimension (inner dimension)?
thanks in advance,
George
|
|
|
|
|
the best ?
std::vector<std::vector<std::string> >
|
|
|
|
|
Thanks toxcct,
I need to use C other than C++. What do you think is the best solution in C?
regards,
George
|
|
|
|
|
Dynamic you can do it something like this:
int nNmbOfStrings = 2;
int nI;
LPTSTR* pArr = new LPTSTR[ nNmbOfStrings ];
pArr[ 0 ] = new TCHAR[ _tcslen( _T("hello") ) + 1 ];
pArr[ 1 ] = new TCHAR[ _tcslen( _T("world") ) + 1 ];
_tcscpy( pArr[ 0 ], _T("hello") );
_tcscpy( pArr[ 1 ], _T("world") );
//deletion:
for ( nI = 0; nI < nNmbOfStrings; nI++ )
{
delete [] pArr[ nI ];
pArr[ nI ] = NULL;
}
delete [] pArr;
pArr = NULL;
|
|
|
|
|
Thanks koos,
It works but a little complex.
regards,
George
|
|
|
|
|
Yep,
It only makes sence when you dynamically want assign/reassign an array.
//f.i. reassign string 2 (before deletion!):
delete [] pArr[ 1 ];
pArr[ 1 ] = new TCHAR[ _tcslen( _T("Every body") ) + 1 ];
_tcscpy( pArr[ 1 ], _T("Every body") );
Regards,
Koos
|
|
|
|
|
Thanks koos,
I am aware that your method has advantage.
regards,
George
|
|
|
|
|
George_George wrote: const char foo[][] = {"hello", "world"}; // compile error
const char goo[][64] = {"hello", "world"}; // compile correc
const char *foo[]= {"hello","world"};
|
|
|
|
|
|
Keep in mind that George's solution is not the same as your original code. Most notably, you shouldn't write to any of the memory occupied by the strings, whereas with regular arrays, it's always safe to write to that memory.
|
|
|
|
|
Thanks Mike,
1. Why his solution is not the same as mine?
2. How memory corruption will occur? Could you let me know an example please? I am using the array as constant string, so I can not imagine a case which will cause memory corruption.
regards,
George
|
|
|
|
|
char s1[] = "foo";
char* s2 = "bar";
strcpy ( s1, "aaa" );
strcpy ( s2, "bbb" ); The memory pointed to by s2 should be treated as read-only.
|
|
|
|