问个关于map的问题,有点迷惑啊
#include <map>
#include <iostream>
using namespace std;
typedef map <int, int> TESTMAP;
typedef vector <int> TESTVEC;
void main()
{
TESTMAP mapOfTest;
mapOfTest[1] = 2;
mapOfTest[2] = 3;
mapOfTest[3] = 4;
mapOfTest[4] = 5;
TESTMAP::iterator itor = mapOfTest.begin();
mapOfTest.erase(itor);
mapOfTest[1] = 2;
itor = mapOfTest.begin();
cout < < itor-> second < < endl;
}
为什么最后mapOfTest.begin()还是2呢?
我希望mapOfTest.begin()是3啊。有什么方法可以将mapOfTest.begin()变成3的啊?
[解决办法]
第二个 mapOfTest[1] = 2; 去掉
[解决办法]
mapOfTest.erase(itor);
mapOfTest[1] = 2;//删除之后这句不是又添加了这个pair吗,当然会再次输出2,把这句去掉,后者换一个其他的
[解决办法]
一般map的实现是红黑树,或者平衡二叉树