【发布时间】:2014-06-18 12:02:59
【问题描述】:
#include <stdio.h>
#include <iostream>
#include <map>
#include <string>
#include <stdlib.h>
using namespace std;
class Fix
{
};
int main()
{
map<int, Fix *> m;
Fix * f = new Fix();
m.insert( make_pair( 2, f) );
m.insert( make_pair( 3, f) );
map<int, Fix *>::iterator it = m.find(2);
map<int, Fix *>::iterator it1 = m.find(2);
m.erase(it);
// Will Create problem
// m.erase(it1);
// Still value is there
// So from the map node, iterator copy its value ?
printf("%d\n", it->first);
printf("%d\n", it1->first);
}
我有一个 Map 包含两个条目,还有两个指向同一个条目的迭代器。 使用 Iterator1 从地图中删除一个条目。删除后仍然 Iterator1 和 Iterator2 持有价值。
问题
- Iterator 是否指向 Map 的节点(红黑树)
- 迭代器在迭代时是否同时处理节点中的键和值?因此,即使在从地图中删除条目后,它也会保留该值。
【问题讨论】: