Question about printing cpp std::map element using "print map1[0]"
Li Guilin via Gdb <[email protected]>
| Newsgroups | gmane.comp.gdb.devel |
|---|---|
| Message-ID | <[email protected]> |
Hello,
I want to print individual std::map element using operator[] instead of
printing the whole map during debugging in gdb, but it is not working.
In contrast, calling operator[] on std::vector and std::deque variables
works fine.
```
(gdb) p map1
$1 = std::map with 1 element = {[0] = 2}
(gdb) p map1[0]
Attempt to take address of value not located in memory.
```
Besides, disabling xmethod and pretty printer does not affet the overall
behavior.
And through "info functions" output, I think gdb knows information about
std::map::operator[], like this (shortened):
```
(gdb) info functions std::map.*operator\[\]
All functions matching regular expression "std::map.*operator\[\]":
File /usr/include/c++/14/bits/stl_map.h:
504: std::map<int, int, ...>::mapped_type &std::map<int, int,
...>::operator[](int const&);
524: std::map<int, int, ...>::mapped_type &std::map<int, int,
...>::operator[](int&&);
```
Below is the sample cpp code.
```
#include <deque>
#include <iostream>
#include <map>
#include <vector>
// instantiate all member functions.
template class std::deque<int>;
template class std::map<int, int>;
template class std::vector<int>;
int main() {
std::deque<int> deque1;
std::map<int, int> map1;
std::vector<int> vector1;
deque1.push_back(1);
std::cout << "deque1[0] = " << deque1[0] << "\n";
map1[0] = 2;
std::cout << "map1[0] = " << map1[0] << "\n";
vector1.push_back(1);
std::cout << "vector1[0] = " << vector1[0] << "\n";
}
```
How to make "print map1[0]" work? Where is the problem?
Thanks a lot for reading this.
Kind regards,
liginity