简体   繁体   English

wstring迭代器的输出上的相等比较不起作用

[英]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. 但是, *it == m_MyString似乎并没有实现字符串相等性- if向量中有匹配的字符串,则不会进入if 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. 因此,它们看上去相同,但实际上并不相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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