[ACCEPTED]-Inspecting STL containers in Visual Studio debugging-stl

Accepted answer
Score: 20

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

(v._Myfirst)[startIndex], count

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

Score: 14

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

Score: 11

In VS2005 and VS 2008 you can see the contents 19 of STL containers. The rules for getting 18 at the data are in autoexp.dat "c:\Program 17 Files\Microsoft Visual Studio 9\Common7\Packages\Debugger\autoexp.dat".

AutoExp.dat 16 is meant to be customized. However, the 15 STL defs are under a section called [Visualizer]. If 14 you can figure out the language used in 13 that section, more power to you, however 12 I'd recommend just leaving that part alone.

Autoexp.dat 11 existed in VS2003, but there was no support 10 for STL containers ([Visualizer] didn't 9 exist). In VS2003 you have to manually 8 navigate the underlying data representation.

By 7 modifying autoexp.dat it is possible to 6 add rules for navigating the data representation 5 of your own types so they are easier to 4 debug. If you do this, you should only 3 add to the stuff under [AutoExp]. Be careful 2 and keep a back up of this file before you 1 modify it.

Score: 8

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

container.operator[](n)
Score: 5

You could create a custom visualiser Check 1 this out: http://www.virtualdub.org/blog/pivot/entry.php?id=120

Score: 4

Most simply method is you have to ready 6 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 5 tried "a._Myfirst[0]" in VisualStudio2015, But 4 It wasn't display array data.

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

This 2 is "sample.natvis" for display 1 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 enter image description here

After enter image description here

Score: 3

Visual Studio 2008, at least for me, displays 2 the contents of STL containers in the standard 1 mouseover contents box.

Score: 1

You can also right-click any value in your 5 watch, and choose 'add watch'. This can 4 be useful if you only need to look at one 3 element of a map or set.

It also leads 2 to the solution that christopher_f posted 1 for vectors - ((v)._Myfirst)[index]

Score: 1

Above discussed method [((v)._Myfirst)[index]] will work only for 18 specific container(std::vector) not for 17 all possible STL containers. For example 16 if you want to see the content of std::deque 15 then you have to look for some other method 14 to browse through the content in std::deque.

Maybe 13 you can try the following similar setting 12 to solve your issue

[I tested this setting only for Visual Studio 2010 Professional version installed with Microsoft Visual studio 2010 Service pack 1]

Step 1: Uninstall the 11 Microsoft Visual studio 2010 Service pack 10 1 - for my project work I don't really need 9 the Service pack 1 so uninstalling service 8 pack 1 will not cause any issue for my case.

Step 7 2: Restart your system.

Step 3: This step 6 is not necessary if you are not getting 5 Error 'LINK : fatal error LNK1123: failure 4 during conversion to COFF: file invalid 3 or corrupt'. Otherwise browse through

Project 2 Property -> Linker (General) -> Change Enable 1 Incremental Linking to No (/INCREMENTAL:NO)

Score: 0

In vs 2015, I could not get any of these 4 working
so, i wrote a bit of code

1: I had vector of vector of long long elements

std::vector<std::string> vs(M_coins + 1);
for (unsigned long long i = 0; i <= M_coins; i++) {
    std::for_each(memo[i].begin(), memo[i].end(), [i, &vs](long long &n) {
        vs[i].append(std::to_string(n));
        });
}
// now vs is ready for use as vs[0], vs[1].. so on, for your debugger

basically 3 what i did was converted vector into string. i 2 had vector of vector so i had string vector 1 to fill.

2: if you have just a vector of long long elements, then just convert as below:

std::vector<std::string> s;
std::for_each(v1.begin(), v1.end(), [&s](long long &n) {
    s.append(std::to_string(n));
    });
// now s is ready for use, for your debugger

hope it helped.

Score: 0

This is old but since I regularly stumble 17 on this post because it is still referenced 16 high on google, as of Visual Studio 2019, one 15 can simply write in the debugger's Watch:

vectorName.data()

(replace 14 vectorName by your variable name) to get 13 a pointer to the content.

Then, knowing the 12 current size of the vector, you can tell 11 the debugger to show you the first N cells:

vectorName.data(),N

(N 10 being the size of your vector)

And if like 9 me, you have a lot of vectors of bytes that 8 actually store another data structure, you 7 can even tell the debugger to interpret 6 the pointer as an array of something else 5 :

(float*)vectorName.data(),4

For example, I have a std::vector of 16 4 bytes and using that I can tell the debugger 3 to show me an array of 4 floats instead 2 (which is more useful to me than the bytes 1 alone).

More Related questions