奇怪的 wstring.find() 行为 | VS C++ 最新
Weird wstring.find() behavior | VS C++ Latest
- 我正在尝试从一些 wstrings 中过滤掉名字,我需要过滤掉一个特定的名字,但由于某种原因,.find() 函数不起作用:
std::wstring buf;
if (buf.find(L"Josh") != std::wstring::npos && !buf.find(L"NotJosh") != std::wstring::npos)
{
// I'm trying to find a specific wstring that contains "Josh" but not "NotJosh"
// yet the .find() func returns positive even if it finds "NotJosh"
// even tho it's conditions is set to (!)
}
/// This one doesn't work either:
if (!buf.find(L"NotJosh") != std::wstring::npos)
{
if (buf.find(L"Josh") != std::wstring::npos)
{
}
}
@Jesper 解决的问题:
This looks fishy: !buf.find(L"NotJosh") != std::wstring::npos
Why not just: buf.find(L"NotJosh") == std::wstring::npos
还有@NathanOliver:
!
has higher precedence then !=
so !buf.find(L"NotJosh") != std::wstring::npos
should be !(buf.find(L"NotJosh") != std::wstring::npos)
谢谢大家
- 我正在尝试从一些 wstrings 中过滤掉名字,我需要过滤掉一个特定的名字,但由于某种原因,.find() 函数不起作用:
std::wstring buf;
if (buf.find(L"Josh") != std::wstring::npos && !buf.find(L"NotJosh") != std::wstring::npos)
{
// I'm trying to find a specific wstring that contains "Josh" but not "NotJosh"
// yet the .find() func returns positive even if it finds "NotJosh"
// even tho it's conditions is set to (!)
}
/// This one doesn't work either:
if (!buf.find(L"NotJosh") != std::wstring::npos)
{
if (buf.find(L"Josh") != std::wstring::npos)
{
}
}
@Jesper 解决的问题:
This looks fishy:
!buf.find(L"NotJosh") != std::wstring::npos
Why not just:buf.find(L"NotJosh") == std::wstring::npos
还有@NathanOliver:
!
has higher precedence then!=
so!buf.find(L"NotJosh") != std::wstring::npos
should be!(buf.find(L"NotJosh") != std::wstring::npos)
谢谢大家