如何用std :: string创build条件断点

假设我有这个function:

std::string Func1(std::string myString) { //do some string processing std::string newString = Func2(myString) return newString; } 

newString具有特定值时,如何设置条件中断? (不改变源)

设置条件newString == "my value"

没有工作的断点得到禁用与错误“重载操作未find”

有些search没有办法做到这一点。 build议的替代方法是将testing放入代码中并添加一个标准断点:

 if (myStr == "xyz") { // Set breakpoint here } 

或者从个人angular色比较来构build你的testing。 即使看string中的单个字符也有点冒险, 在Visual Studio 2005中,我不得不深入到像成员variables

 myStr._Bx._Buf[0] == 'x' && myStr._Bx._Buf[1] == 'y' && myStr._Bx._Buf[2] == 'z' 

这两种方法都不是很令人满意。 我们应该更好地利用标准库的无处不在的function。

Visual Studio 2010/2012中有一个更简单的方法。

为了在ANSI中完成你所需要的,使用这个:

 strcmp(newString._Bx._Ptr,"my value")==0 

而在unicode(如果newString是unicode)使用这个:

 wcscmp(newString._Bx._Ptr, L"my value")==0 

你可以做更多的事情,而不仅仅是比较,你可以在这里阅读更多的信息:

http://blogs.msdn.com/b/habibh/archive/2009/07/07/new-visual-studio-debugger-2010-feature-for-cc-developers-using-string-functions-in-conditional- breakpoints.aspx

虽然我不得不使用与Brad的答案类似的东西(再加上使用DebugBreak()从代码中分离出来),但有时编辑/重新编译/重新运行一些代码要么太耗时,要么根本不可能。

幸运的是,显然可以拼成std :: string类的实际成员。 这里提到了一种方法 – 尽pipe他专门调用了VS2010,但仍然可以在早期版本中手动访问各个字符。 所以,如果你使用的是2010年,你可以使用strcmp()函数等( 更多信息) ,但是如果你像我一样,仍然有2008年或更早的版本,你可以想出一个糟糕的,但通过设置一个断点的function替代条件如下:

 strVar._Bx._Ptr[0] == 'a' && strVar._Bx._Ptr[1] == 'b' && strVar._Bx._Ptr[2] == 'c' 

如果strVar中的前三个字符是“abc”则打破。 当然,你可以继续使用额外的字符。 丑陋..但是现在就救了我一点时间。

VS2012:

我只是使用下面的条件,因为newString._Bx._Ptr(如在OBWANDO的答案)引用非法内存

 strcmp( newString._Bx._Buf, "my value")==0 

它的工作…

在VS2017你可以做

 strcmp(newString._Mypair._Myval2._Bx._Buf,"myvalue")==0 

在VS2015你可以做

 newstring[0]=='x' && newString[1]=='y' && newString[2]=='z'