C++ 在 Visual Studio 调试中检查 STL 容器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/104959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:52:35  来源:igfitidea点击:

Inspecting STL containers in Visual Studio debugging

c++visual-studiodebuggingstl

提问by kevin42

If I have a std::vectoror std::mapvariable, 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)?

如果我有一个std::vectororstd::map变量,并且我想查看内容,那么在调试时看到第 n 个元素是一件非常痛苦的事情。是否有插件或一些技巧可以在调试时更轻松地查看 STL 容器变量(VS2003/2005/2008)

采纳答案by christopher_f

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

对于向量,msdn 论坛上的这个线程有一个代码片段,用于在向量索引上设置监视可能会有所帮助。

回答by Adam Rosenfield

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

(v._Myfirst)[startIndex], count

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

但是请注意,count 必须是常量,不能是其他表达式的结果。

回答by Steve Steiner

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".

在 VS2005 和 VS 2008 中,您可以看到 STL 容器的内容。获取数据的规则在 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 旨在定制。但是,STL 定义位于名为 [Visualizer] 的部分下。如果您能弄清楚该部分中使用的语言,则会为您提供更多功能,但是我建议您不要理会那部分。

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.

Autoexp.dat 存在于 VS2003 中,但不支持 STL 容器([Visualizer] 不存在)。在 VS2003 中,您必须手动导航底层数据表示。

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.

通过修改 autoexp.dat,可以添加用于导航您自己类型的数据表示的规则,以便它们更易于调试。如果你这样做,你应该只添加到 [AutoExp] 下的东西。在修改该文件之前,请小心并备份该文件。

回答by 1''

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

要在 Visual Studio 调试器中查看容器的第 n 个元素,请使用:

container.operator[](n)

回答by maxbog

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

你可以创建一个自定义的可视化工具看看这个:http: //www.virtualdub.org/blog/pivot/entry.php?id=120

回答by 24k.wakahana

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.

我在 VisualStudio2015 中尝试了“a._Myfirst[0]”,但它不是显示数组数据。

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

如果您可以使用“ natvis”,它将解决您的问题。

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

这是用于显示 Visual Studio 2015 的 std::vector 数据的“sample.natvis”。

<?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>

Beforeenter image description here

在此处输入图片说明

Afterenter image description here

在此处输入图片说明

回答by anon6439

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

至少对我而言,Visual Studio 2008 在标准鼠标悬停内容框中显示 STL 容器的内容。

回答by tfinniga

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]

这也导致了 christopher_f 为向量发布的解决方案 - ((v)._Myfirst)[index]

回答by SridharKritha

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.

上面讨论的方法[((v)._Myfirst)[index]]仅适用于特定容器(std::vector),而不适用于所有可能的 STL 容器。例如,如果您想查看 std::deque 的内容,那么您必须寻找其他方法来浏览 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]

[我仅针对随 Microsoft Visual Studio 2010 Service Pack 1 安装的 Visual Studio 2010 Professional 版本测试了此设置]

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.

步骤 1:卸载 Microsoft Visual Studio 2010 Service Pack 1 - 对于我的项目工作,我并不真正需要 Service Pack 1,因此卸载 Service Pack 1 不会对我的情况造成任何问题。

Step 2: Restart your system.

第 2 步:重新启动系统。

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

第 3 步:如果您没有收到错误“链接:致命错误 LNK1123:转换为 COFF 期间失败:文件无效或损坏”,则不需要此步骤。否则浏览

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

项目属性 -> 链接器(常规) -> 将启用增量链接更改为否 (/INCREMENTAL:NO)

回答by Manohar Reddy Poreddy

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

在 vs 2015 中,我无法让任何这些工作,
所以我写了一些代码

1: I had vector of vector of long long elements

1:我有长长元素的向量

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.

基本上我所做的是将向量转换为字符串。我有向量的向量,所以我有字符串向量来填充。

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

2:如果你只有一个 long long 元素的向量,那么只需转换如下:

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.

希望它有所帮助。