I'm using Visual Studio 2013 and in its long history it was never able to show a vector element in debugger, complaining with no operator "[]" matches these operands
message. I know that there is a workaround requiring typing v.operator[](n)
, but this is not acceptable for me. I want to hover the cursor above v[n]
and see its value or at the most select or cut and paste v[n]
to see the value. Is it possible with other Windows C++ IDEs?
I know that all elements of vector are shown in Autos
and Locals
windows, but my vectors are too long for this to be practical.
Vector elements are accessed using indexing vectors, which can be numeric, character or logical vectors. You can access an individual element of a vector by its position (or "index"), indicated using square brackets. In R, the first element has an index of 1.
Starting with Visual C++ version 6.0, it's now possible to expand an array pointer to view all array elements in the Visual C++ debugger Watch window. This feature isn't documented. In the Watch window, type an expression that evaluates to a pointer followed by a comma and the number of elements in the array.
get() method is used to fetch or retrieve an element at a specific index from a Vector.
Use std::find_if() with std::distance() This is a recommended approach to finding an element in a vector if the search needs to satisfy a specific logic eg, finding an index of element in the vector that is a prime number using prime number logic.
Just prefix each []
with _Myfirst
in the Watch field:
YourVector._Myfirst[n]
Trick here:
Say you have an std::vector<int> v;
and you want to see in the watch v[23]
or maybe v[23]..v[23+n]
do this:
,!
after the name of the variable (ex: v,!
) this indicate VS that you want to turn off debugger visualization._Myfirst
, _Mylast
and _Myend
. Add _Myfirst
to the watch. This is the pointer to the beginning of the vector memory.v,!
from the watch if you want._Myfirst
element added to the watch add at the end + offset, count
where offset is the vector index you want to see first in the watch and count is the numbers of element of the vector you want to see. Would be something like this: (*((std::_Vector_val<std::_Simple_types<int> >*)(&(*((std::_Vector_alloc<0,std::_Vec_base_types<int,std::allocator<int> > >*)(&(v)))))))._Myfirst + 23, 100
. This let you see 100 elements of the vector starting in position 23 (yes I known it's large the _Myfirst element). You could specify offset
and count
using variables (ex: to match an expression in the code like v[n]
use as offset n
and count
whatever you want, constant or variable.Some info about Debugging Tips and Tricks, Going Native Episode 28 from minute 17 have some goodies, the evaluation expression could be in comments. Example you have some code.
v[n] = ... + pre_calculate(v[n]) + ...
// You could put a comment like this:
// (*((std::_Vector_val<std::_Simple_types<int> >*)(&(*((std::_Vector_alloc<0,std::_Vec_base_types<int,std::allocator<int> > >*)(&(v)))))))._Myfirst + n, 100
// And when you hover the mouse over the selected expression, you see the evaluation. Much better I think.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With