Inspecting STL containers in Visual Studio debugging

前端 未结 10 1347
旧巷少年郎
旧巷少年郎 2020-12-05 06:52

If I have a std::vector or std::map variable, and I want to see the contents, it\'s a big pain to see the nth element while debugging. Is there a p

相关标签:
10条回答
  • 2020-12-05 07:31

    For vectors, this thread on the msdn forums has a code snippet for setting a watch on a vector index that might help.

    0 讨论(0)
  • 2020-12-05 07:34

    To view the nth element of a container in the Visual Studio debugger, use:

    container.operator[](n)
    
    0 讨论(0)
  • 2020-12-05 07:40

    If you want to watch more than one element at the same time, you can append a comma and the number of elements as so:

    (v._Myfirst)[startIndex], count

    However, note that count must be a constant, it cannot be the result of another expression.

    0 讨论(0)
  • 2020-12-05 07:43

    Most simply method is you have to ready a pointer to watch variable like this.

    vector<int> a = { 0,1,2,3,4,5 };
    int* ptr = &a[0]; // watch this ptr in VisualStudio Watch window like this "ptr,6".
    

    I tried "a._Myfirst[0]" in VisualStudio2015, But It wasn't display array data.

    If you can use "natvis", it will resolve your problems.

    This is "sample.natvis" for display std::vector data for Visual studio 2015.

    <?xml version="1.0" encoding="utf-8"?> 
    <AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
      <Type Name="std::vector&lt;*&gt;">
        <DisplayString>{{ size={_Mypair._Myval2._Mylast - _Mypair._Myval2._Myfirst} }}</DisplayString>
        <Expand>
          <Item Name="[size]" ExcludeView="simple">_Mypair._Myval2._Mylast - _Mypair._Myval2._Myfirst</Item>
          <Item Name="[capacity]" ExcludeView="simple">_Mypair._Myval2._Myend - _Mypair._Myval2._Myfirst</Item>
          <ArrayItems>
            <Size>_Mypair._Myval2._Mylast - _Mypair._Myval2._Myfirst</Size>
            <ValuePointer>_Mypair._Myval2._Myfirst</ValuePointer>
          </ArrayItems>
        </Expand>
      </Type>
    </AutoVisualizer>
    

    Before

    After

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