string.find()错误地返回true

时间:2016-06-26 21:50:20

标签: c++

代码解释了我想要做的事情

auto haystack = wstring(L"\REGISTRY\MACHINE\SOFTWARE\WOW6432Node\Data\0\Path");
auto needle = wstring(L"\WOW6432Node\Data\0\Name");
auto found = haystack.find(needle) != haystack.npos;
cout << found << endl;

它返回true虽然needlehaystack不同,但注意前者以\Path结尾,而后者以\Name结尾...我怎么能确切地说出是否确定string包含另一个?

3 个答案:

答案 0 :(得分:2)

由于终止&#39; \ 0&#39;字符串比你想象的要短:

href="management/users"

其他非预期的转义序列应该产生编译器警告:

g ++:警告:未知的转义序列:&#39; \ R&#39; (......等等)

您可以查看:http://en.cppreference.com/w/cpp/language/string_literal

答案 1 :(得分:0)

frameBuffer[0] = modify(audioData[0]); // left sample 1 frameBuffer[1] = modify(audioData[1]); // right sample 1 frameBuffer[2] = modify(audioData[2]); // left sample 2 frameBuffer[3] = modify(audioData[3]); // right sample 2 // ... frameBuffer[2*(n-1)] = modify(audioData[2*(n-1)]); // left sample n frameBuffer[2*(n-1)+1] = modify(audioData[2*(n-1)+1]); // right sample n 字符是转义字符。要使用实际的\字符,您需要使用\来转义它所以您的代码需要像这样更改:

\\.

答案 2 :(得分:0)

你可以这样做:

wstring haystack = wstring(LR"P(\REGISTRY\MACHINE\SOFTWARE\WOW6432Node\Data\0\Path)P");
wstring needle = wstring(LR"P(\WOW6432Node\Data\0\Name)P");
bool found = haystack.find(needle) != haystack.npos;
cout << (char*)(found?"True":"False") << endl;

\ 0对你来说是一个问题,因为它过早地结束了构造函数