how to create conditional breakpoint with std::string

前端 未结 11 996
粉色の甜心
粉色の甜心 2020-12-07 12:54

Suppose I have this function:

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


        
相关标签:
11条回答
  • 2020-12-07 13:50

    You could convert it into a c string using c_str() like so:

    $_streq(myStr.c_str(), "foo")

    0 讨论(0)
  • 2020-12-07 13:52

    There is a much easier way in Visual Studio 2010/2012.

    To accomplish what you are looking for in ANSI use this:

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

    And in unicode (if newString were unicode) use this:

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

    There are more things you can do than just a compare, you can read more about it here:

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

    0 讨论(0)
  • 2020-12-07 13:53

    Some searching has failed to turn up any way to do this. Suggested alternatives are to put the test in your code and add a standard breakpoint:

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

    Or to build up your test from individual character comparisons. Even looking at individual characters in the string is a bit dicey; in Visual Studio 2005 I had to dig down into the member variables like

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

    Neither of these approaches is very satisfactory. We should have better access to a ubiquitous feature of the Standard Library.

    0 讨论(0)
  • 2020-12-07 13:54

    VS2012:

    I just used the condition below because newString._Bx._Ptr ( as in OBWANDO's answer ) referenced illegal memory

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

    and it worked...

    0 讨论(0)
  • 2020-12-07 13:59

    While I've had to work around this using something similar to Brad's answer (plus using DebugBreak() to break right from the code), sometimes editing/recompiling/re-running a bit of code is either too time consuming or just plain impossible.

    Luckily, it's apparently possible to spelunk into the actual members of the std::string class. One way is mentioned here -- and though he calls out VS2010 specifically, you can still access individual chars manually in earlier versions. So if you're using 2010, you can just use the nice strcmp() functions and the like (more info), but if you're like me and still have 2008 or earlier, you can come up with a raggedy, terrible, but functional alternative by setting a breakpoint conditional something like:

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

    to break if the first three characters in strVar are "abc". You can keep going with additional chars, of course. Ugly.. but it's saved me a little time just now.

    0 讨论(0)
提交回复
热议问题