Reputation: 2128
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 plugin, or some trick to making it easier to watch STL container variables while debugging (VS2003/2005/2008)
?
Upvotes: 36
Views: 37599
Reputation: 1
Apart from other answers I have an easy solution if your code is not proprietary.
I use online debugger to debug c++ code for programming problems it shows all stl values by default.
Upvotes: 0
Reputation: 487
This is old but since I regularly stumble on this post because it is still referenced high on google, as of Visual Studio 2019, one can simply write in the debugger's Watch:
vectorName.data()
(replace vectorName by your variable name) to get a pointer to the content.
Then, knowing the current size of the vector, you can tell the debugger to show you the first N cells:
vectorName.data(),N
(N being the size of your vector)
And if like me, you have a lot of vectors of bytes that actually store another data structure, you can even tell the debugger to interpret the pointer as an array of something else :
(float*)vectorName.data(),4
For example, I have a std::vector of 16 bytes and using that I can tell the debugger to show me an array of 4 floats instead (which is more useful to me than the bytes alone).
Upvotes: 0
Reputation: 41
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>
Upvotes: 4
Reputation: 27465
In vs 2015, I could not get any of these working
so, i wrote a bit of code
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 what i did was converted vector into string. i had vector of vector so i had string vector to fill.
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.
Upvotes: 0
Reputation: 27105
To view the nth element of a container in the Visual Studio debugger, use:
container.operator[](n)
Upvotes: 8
Reputation: 9671
Above discussed method [((v)._Myfirst)[index]] will work only for specific container(std::vector) not for all possible STL containers. For example if you want to see the content of std::deque then you have to look for some other method to browse through the content in std::deque.
Maybe you can try the following similar setting 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 Microsoft Visual studio 2010 Service pack 1 - for my project work I don't really need the Service pack 1 so uninstalling service pack 1 will not cause any issue for my case.
Step 2: Restart your system.
Step 3: This step is not necessary if you are not getting Error 'LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt'. Otherwise browse through
Project Property -> Linker (General) -> Change Enable Incremental Linking to No (/INCREMENTAL:NO)
Upvotes: 1
Reputation: 5369
In VS2005 and VS 2008 you can see the contents of STL containers. The rules for getting at the data are in autoexp.dat "c:\Program Files\Microsoft Visual Studio 9\Common7\Packages\Debugger\autoexp.dat".
AutoExp.dat is meant to be customized. However, the STL defs are under a section called [Visualizer]. If you can figure out the language used in that section, more power to you, however I'd recommend just leaving that part alone.
Autoexp.dat existed in VS2003, but there was no support for STL containers ([Visualizer] didn't exist). In VS2003 you have to manually navigate the underlying data representation.
By modifying autoexp.dat it is possible to add rules for navigating the data representation of your own types so they are easier to debug. If you do this, you should only add to the stuff under [AutoExp]. Be careful and keep a back up of this file before you modify it.
Upvotes: 11
Reputation: 400454
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.
Upvotes: 20
Reputation: 6859
You can also right-click any value in your watch, and choose 'add watch'. This can be useful if you only need to look at one element of a map or set.
It also leads to the solution that christopher_f posted for vectors - ((v)._Myfirst)[index]
Upvotes: 1
Reputation: 637
You could create a custom visualiser Check this out: http://www.virtualdub.org/blog/pivot/entry.php?id=120
Upvotes: 5
Reputation: 1897
Visual Studio 2008, at least for me, displays the contents of STL containers in the standard mouseover contents box.
Upvotes: 3
Reputation: 1985
For vectors, this thread on the msdn forums has a code snippet for setting a watch on a vector index that might help.
Upvotes: 14