关于使用map.insert 的一个问题
#include "stdafx.h"
#include <string>
#include <map>
#include <iostream>
using namespace std;
class CPerson
{
private:
string m_firstName;
string m_secondName;
public:
CPerson (string fname,string sname):m_firstName(fname),m_secondName(sname)
{
}
void ShowPerson() const
{
cout<<"first name is :"<<m_firstName<<endl;
cout<<"second name is :"<<m_secondName<<endl;
}
//如果要往map中insert数据必须实现比较操作符,因为map中insert函数的第三个参数
//是less<T> 默认按照升序进行Insert数据,而且要求能被常量对象调用
bool operator>(const CPerson &person) const
{
return person.m_firstName>this->m_firstName;
}
bool operator<(const CPerson &person) const
{
return this->m_firstName<person.m_firstName;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
map<CPerson,string>map_phoenbooks;
typedef pair<CPerson ,string> ITEM;
ITEM item = pair<CPerson ,string>(CPerson("fname1","sname"),"123456");
//这样写没问题
pair<map<CPerson,string>::iterator ,bool> &checkpair = map_phoenbooks.insert(item);
checkpair.first->first.ShowPerson();
//这样写在debug模式下内存错误,在release模式下正常
pair<map<CPerson,string>::iterator ,bool> *pcheckpair = &map_phoenbooks.insert(item);
pcheckpair->first->first.ShowPerson();
int i=0;
cin>>i;
return 0;
}
跟踪到STL XTREE 源码中
是一下代码 中_Mycnt为Null
#if _HAS_ITERATOR_DEBUGGING
if (this->_Mycont == 0
|| _Ptr == 0
|| _Ptr == ((_Myt *)this->_Mycont)->_Myhead)
{
_DEBUG_ERROR("map/set iterator not dereferencable");
_SCL_SECURE_OUT_OF_RANGE;
}
请问这是为什么? 是不是map不能使用指针去访问Pair?