C++ How do I get a reference to a nested map, to remove an element?

  c++, c++98

How do I get a reference to the second map here? I want to be able to delete elements by using items.erase(). In the example below, the number of elements in the main map doesn’t change, it only changes the copy. C++ 98 only please.

#include <iostream>
#include <map>
#include <string>

using namespace std;

int main()
{
    map<int, map<int, string> > acks;
    acks[10].insert(make_pair(1, "a"));
    acks[10].insert(make_pair(2, "b"));
    acks[10].insert(make_pair(3, "c"));

    acks[20].insert(make_pair(11, "b"));
    acks[20].insert(make_pair(12, "c"));
    
    map<int, map<int, string> >::const_iterator it = acks.find(10);
    if (it != acks.end())
    {
        map<int, string> items = it->second;
        map<int, string>::const_iterator it2 = items.find(3);
        cout << "size before: " << acks[10].size() << endl;
        items.erase(it2);
        cout << "size after: " << acks[10].size() << endl;
    }
}

Source: Windows Questions C++

LEAVE A COMMENT