要在 std::string 中设置条件断点,您需要在 std::string 的真实内部成员上设置它。您在监视窗口中看到的内容已简化。
您可以使用,! 后缀在监视窗口中显示变量的真实结构。在您的示例中:
newString,!
对于 MSVC 2015 – 2019,您可以使用:
对于不超过 15 个字符的字符串:
(newString._Mypair._Myval2._Myres < 16) ?
strcmp(newString._Mypair._Myval2._Bx._Buf, "short") == 0 :
false
对于(甚至历史上)更长的字符串:
(newString._Mypair._Myval2._Myres < 16) ? false :
strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_str_value_longer_than_16_chars") == 0
小心:
- 变量名在每个条件下写两次!
- 您需要单行的完整表达式。使用下面的复制粘贴版本。
万能条件需要把测试值放两次,变量名放三次:
(newString._Mypair._Myval2._Myres < 16) ?
strcmp(newString._Mypair._Myval2._Bx._Buf, "My_test_string") == 0 :
strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_string") == 0
注意:如果您使用的是std::wstring,请使用wcscmp 而不是strcmp。
查找更多关于 C++ 中小字符串优化的信息https://vorbrodt.blog/2019/03/30/sso-of-stdstring/ 包括用于查找字符串内部缓冲区大小的示例代码。
所有 std:string 和 std::wstring 单行版本,方便您复制粘贴:
(newString._Mypair._Myval2._Myres < 16) ? strcmp(newString._Mypair._Myval2._Bx._Buf, "short") == 0 : false
(newString._Mypair._Myval2._Myres < 16) ? false : strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_str_value_longer_than_16_chars") == 0
(newString._Mypair._Myval2._Myres < 16) ? strcmp(newString._Mypair._Myval2._Bx._Buf, "My_test_string") == 0 : strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_string") == 0
(newString._Mypair._Myval2._Myres < 16) ? wcscmp(newString._Mypair._Myval2._Bx._Buf, L"short") == 0 : false
(newString._Mypair._Myval2._Myres < 16) ? false : wcscmp(newString._Mypair._Myval2._Bx._Ptr, L"My_test_str_value_longer_than_16_chars") == 0
(newString._Mypair._Myval2._Myres < 16) ? wcscmp(newString._Mypair._Myval2._Bx._Buf, L"My_test_string") == 0 : wcscmp(newString._Mypair._Myval2._Bx._Ptr, L"My_test_string") == 0
以上所有复制/粘贴示例均在 MSVC 版本 16.9.10 和适用于 Windows 10 的程序上测试。