W
webretard
I never used C much and have grown accustomed to C++ std::string. This
is a weakness because I don't understand why comparisons ( == ) are
unreliable when dealing only with char[] types. It seems std::string
is always comparable and that std::string and char[] are always
comparable, but char[] to char[] comparison works sometimes, but not
others depending on the content of the char[]. Here is a small snippet
I wrote to demonstrate:
int main()
{
std::string one = "1qaz2wsx";
std::string two = "1qaz2wsx";
// Always matches
if ( one == two )
{
std::cout << "std::string Match" << std::endl;
std::cout << one << std::endl;
std::cout << two << std::endl;
}
else
{
std::cout << "std::string No Match" << std::endl;
std::cout << one << std::endl;
std::cout << two << std::endl;
}
// ------------------------------------------------------
char three[9] = "1qaz2wsx";
char four[9] = "1qaz2wsx";
// Matches sometimes, not others
if ( three == four )
{
std::cout << "char Match" << std::endl;
std::cout << three << std::endl;
std::cout << four << std::endl;
}
else
{
std::cout << "char No Match" << std::endl;
std::cout << three << std::endl;
std::cout << four << std::endl;
}
// ------------------------------------------------------
// Always matches
if ( one == three )
{
std::cout << "std::string and char Match" << std::endl;
std::cout << three << std::endl;
std::cout << four << std::endl;
}
else
{
std::cout << "std::string and char No Match" << std::endl;
std::cout << three << std::endl;
std::cout << four << std::endl;
}
return 0;
}
My goal is to compare char[] just as reliably as I can compare
std::string objects.
Terry
is a weakness because I don't understand why comparisons ( == ) are
unreliable when dealing only with char[] types. It seems std::string
is always comparable and that std::string and char[] are always
comparable, but char[] to char[] comparison works sometimes, but not
others depending on the content of the char[]. Here is a small snippet
I wrote to demonstrate:
int main()
{
std::string one = "1qaz2wsx";
std::string two = "1qaz2wsx";
// Always matches
if ( one == two )
{
std::cout << "std::string Match" << std::endl;
std::cout << one << std::endl;
std::cout << two << std::endl;
}
else
{
std::cout << "std::string No Match" << std::endl;
std::cout << one << std::endl;
std::cout << two << std::endl;
}
// ------------------------------------------------------
char three[9] = "1qaz2wsx";
char four[9] = "1qaz2wsx";
// Matches sometimes, not others
if ( three == four )
{
std::cout << "char Match" << std::endl;
std::cout << three << std::endl;
std::cout << four << std::endl;
}
else
{
std::cout << "char No Match" << std::endl;
std::cout << three << std::endl;
std::cout << four << std::endl;
}
// ------------------------------------------------------
// Always matches
if ( one == three )
{
std::cout << "std::string and char Match" << std::endl;
std::cout << three << std::endl;
std::cout << four << std::endl;
}
else
{
std::cout << "std::string and char No Match" << std::endl;
std::cout << three << std::endl;
std::cout << four << std::endl;
}
return 0;
}
My goal is to compare char[] just as reliably as I can compare
std::string objects.
Terry