简体   繁体   中英

Equality comparison on the output of a wstring iterator not working

I've got a vector holding wstrings. I want to see if one of the strings has the same contents as a string in a variable:

std::wstring m_MyString;

void CheckExists() {
    std::vector< std::wstring > list = ...
    for (std::vector< std::wstring >::iterator it = list.begin(); it != list.end(); ++it) {
        if (*it == m_MyString) {
            ...
            break;
        }
    }
}

However, the *it == m_MyString doesn't seem to be doing string equality - it doesn't go into the if when there's a matching string in the vector. I think it's doing pointer/address equality. How do I get it to do basic string equality?

Turns out the problem was that the string in the variable was null-terminated, the strings in the vector weren't. So they looked the same, but weren't actually the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM