As Element at index in a std::set? explains, there is no "direct" random access by index in a std::set
- so, here I'm trying to use it's .begin()
method that returns an iterator... Here is a simple minimal example:
// g++ --std=c++11 -g test.cpp -o test.exe
#include <iostream>
#include <set>
int main()
{
std::set<std::string> my_set;
my_set.insert("AA");
my_set.insert("BB");
std::cout << "Hello World!" << std::endl;
return 0;
}
What I ultimately want to do is use a gdb
dprintf
type break in my actual problem - I would like to not change the code, and thus not add additional iterator variables. Since dprintf
uses format specifiers, where %s
is for a C-style string, I ultimately need not just a reference to the first element of my_set
, but I'd also need to call .c_str()
on it - and all of this in a one-liner. (in my actual problem, I have a std::set
of a custom String class, which has a custom method for getting a C-style string).
The problem is, I cannot find the right syntax to access the first element:
$ gdb --args ./test.exe
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
...
Reading symbols from ./test.exe...done.
(gdb) b test.cpp:11
Breakpoint 1 at 0x8048bf8: file test.cpp, line 11.
(gdb) r
Starting program: /tmp/test.exe
Breakpoint 1, main () at test.cpp:11
11 std::cout << "Hello World!" << std::endl;
(gdb) p my_set
$1 = std::set with 2 elements = {[0] = "AA", [1] = "BB"}
(gdb) p my_set.begin()
Cannot evaluate function -- may be inlined
(gdb) p my_set->begin()
Cannot resolve method std::set<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::begin to any overloaded instance
(gdb) printf "%s", my_set.begin()
Cannot evaluate function -- may be inlined
Is it possible to print the first value of a std::set
in a context like this using gdb
's printf
, without having to change the code (for instance, to add iterator variables)?