关于使用map.insert 的一个问题

n27741 2011-10-23 01:01:37
#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?
...全文
295 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
n27741 2011-10-23
  • 打赏
  • 举报
回复
用的vc2008

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧