gdb로 std::map에 인덱스를 이용해서 바로 값에 접근하는 방법이 있나요?

조회수 666회
#include <iostream>
#include <map>
#include <string>

using std::cout;
using std::map;
using std::string;
using std::to_string;

int main()
{
    map<string, int> si;

    for(int i = 0; i < 10; ++i)
    {
        si[to_string(i)] = i;
    }

    for(int i = 0; i < 10; ++i)
    {
        cout << si[to_string(i)] << "\n";
    }
}

이 코드를 -g 옵션을 주고 빌드한 후에 gdb로 실행해서 si["1"] 이렇게 하면 invalid cast 오류가 나네요..

GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ctest...
(gdb) l main
6       using std::map;
7       using std::string;
8       using std::to_string;
9
10      int main()
11      {
12              map<string, int> si;
13
14              for(int i = 0; i < 10; ++i)
15              {
(gdb) l 15
10      int main()
11      {
12              map<string, int> si;
13
14              for(int i = 0; i < 10; ++i)
15              {
16                      si[to_string(i)] = i;
17              }
18
19              for(int i = 0; i < 10; ++i)
(gdb) b 18
Breakpoint 1 at 0x2579: file ctest.cpp, line 19.
(gdb) r
Starting program: /home/lksj/ctest

Breakpoint 1, main () at ctest.cpp:19
19              for(int i = 0; i < 10; ++i)
(gdb) p si["1"]
Invalid cast.
(gdb)

map 오브젝트에 넣어둔 인덱스로 직접 값에 접근할 수 있는 방법이 있을까요?

  • (•́ ✖ •̀)
    알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)