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
For vectors, this thread on the msdn forums has a code snippet for setting a watch on a vector index that might help.
To view the nth element of a container in the Visual Studio debugger, use:
container.operator[](n)
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.
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<*>">
<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