Thread: std::string::operator== fails for equal strings?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    std::string::operator== fails for equal strings?

    Hi,

    in this simple peace of code I compare std::string's for equality:

    Code:
    const int
    UserTypes::getInt(const std::string& str) const throw(NoExistingTypeException)
    {
    	for
    	(
    		std::map<int, std::string>::const_iterator citer = mUserTypesMap.begin();
    		citer != mUserTypesMap.end();
    		++citer
    	)
    	{
    		if(citer->second == str)
    		{
    			return citer->first;
    		}
    	}
    	
    	throw NoExistingTypeException();
    }
    I've checked the values of the two compared strings inside the debugger. They are 100% equal (or I got crazy somehow) but the operator returns false. I guess I'm doing something really stupid but I'm unable to see what this morning.
    Thank you in advance!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I doubt they are identical. Are you sure you are not missing some unprintable last character on one side or the other. Are the lengths the same?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They can't be equal if operator == fails.
    Check for case error, mismatches characterd (I and l maybe?), and hidden characters.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    You are right, one of the strings had an additional character (0x00) appended. Thanks

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by pheres View Post
    You are right, one of the strings had an additional character (0x00) appended. Thanks
    A great piece of evidence that std::string is not a null-terminated format. The presence of the null terminator made a difference.

    I suspect that some piece of code which converts from C-style string to std::string is erroneously including the null terminator in the total count.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Exactly. I wrongly passed strlen(char_array)+1 to the string-constructor somethere. C-strings are a bit confusing sometimes if one is not used to them and has to interface some c-api from time to time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM