I have to create an iterator to the stack but before pushing some values into the stack, after struggling for some time I figured out why following chunk of code causes seg fault
#include <map>
#include <set>
#include <stack>
#include <utility>
#include <algorithm>
typedef std::stack<std::pair<short, std::set<short>>> stack_t;
int main()
{
std::set<short> x = {0, 1, 2, 3};
stack_t my_stack;
auto iter = my_stack.top(); // error (segmentation fault)
my_stack.push(std::make_pair(3, x));
iter = my_stack.top();
//...
return 0;
}
is there a way to create an iterator before initializing the stack?