在 Visual Studio 代码中调试(C++ 程序)时看不到矢量或其他容器(例如地图)的内容
Can't see the contents of vector or other containers (e.g map) while debugging (C++ program) in Visual Studio Code
我编写了一个简单的 C++(11)
程序来测试 Windows 中 Visual studio 代码中的调试器。我正在使用 MinGW 发行版作为编译器选项。
我已经按照此处的文档设置了 tasks.json
和 launch.json
:
https://code.visualstudio.com/docs/cpp/config-mingw
这里是示例 cpp 代码:
typedef std::vector<int> vi;
int main(int argc, char const *argv[])
{
fast();
int tests = 1;
cin >> tests;
while(tests--)
{
int n = 5, k = 25;
cin >> n >> k;
vi permutation(n);
for (int i = 0; i < n; i++)
{
cin >> permutation[i];
}
// vi permutation = {5,4,3,2,1};
auto ans = solve(permutation, k);
// other logic goes here
}
return 0;
}
我的 tasks.json
文件:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\MinGW\bin\g++.exe",
"args": [
"-g",
"-std=c++11",
"${file}",
"-o",
"${fileDirname}\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\MinGW\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": false,
"reveal": "always",
"focus": true,
"panel": "shared",
"clear": false,
"showReuseMessage": false
},
}
]
}
我的launch.json
文件:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\MinGW\bin\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
我在调试器中看到了这个 window:
根据文档,我应该会看到如下所示的屏幕:
我是不是设置错了?为什么我看不到容器的内容?我观察到我也看不到其他容器的值。
我从您提到的站点安装 MINGW 时遇到了同样的问题。然后我从 SourceForge mentioned in the wiki 安装了 MINGW,它开始工作正常。请试一试。
我编写了一个简单的 C++(11)
程序来测试 Windows 中 Visual studio 代码中的调试器。我正在使用 MinGW 发行版作为编译器选项。
我已经按照此处的文档设置了 tasks.json
和 launch.json
:
https://code.visualstudio.com/docs/cpp/config-mingw
这里是示例 cpp 代码:
typedef std::vector<int> vi;
int main(int argc, char const *argv[])
{
fast();
int tests = 1;
cin >> tests;
while(tests--)
{
int n = 5, k = 25;
cin >> n >> k;
vi permutation(n);
for (int i = 0; i < n; i++)
{
cin >> permutation[i];
}
// vi permutation = {5,4,3,2,1};
auto ans = solve(permutation, k);
// other logic goes here
}
return 0;
}
我的 tasks.json
文件:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\MinGW\bin\g++.exe",
"args": [
"-g",
"-std=c++11",
"${file}",
"-o",
"${fileDirname}\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\MinGW\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": false,
"reveal": "always",
"focus": true,
"panel": "shared",
"clear": false,
"showReuseMessage": false
},
}
]
}
我的launch.json
文件:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\MinGW\bin\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
我在调试器中看到了这个 window:
根据文档,我应该会看到如下所示的屏幕:
我是不是设置错了?为什么我看不到容器的内容?我观察到我也看不到其他容器的值。
我从您提到的站点安装 MINGW 时遇到了同样的问题。然后我从 SourceForge mentioned in the wiki 安装了 MINGW,它开始工作正常。请试一试。