日期:2014-05-16  浏览次数:20990 次

Segmentation fault (core dumped)
#include <iostream>
#include <string>
//#include <UTILITY>
#include <map>
using namespace std;
struct userdevice
{
   /* data */
   string m_devicename;
   long m_deviced;
   int m_devicePopedom;
};

typedef multimap<string,userdevice> USERTABLE;
typedef USERTABLE::const_iterator CIT;
typedef pair<CIT,CIT> Range;
int main()
{
   CIT it;
userdevice d1,d2,d3,d4;
d1.m_devicename="d1";
d1.m_deviced=12341234;
d1.m_devicePopedom=123;

d2.m_devicename="d2";
d2.m_deviced=23622344;
d2.m_devicePopedom=234;


d3.m_devicename="d3";
d3.m_deviced=23451234;
d3.m_devicePopedom=345;


d4.m_devicename="d4";
d4.m_deviced=43622344;
d4.m_devicePopedom=456;

USERTABLE m_user;
m_user.insert(pair<string,userdevice> ("zhangsanfeng",d1));
m_user.insert(pair<string,userdevice> ("zhangsanfeng",d2));
m_user.insert(pair<string,userdevice> ("zhangsanfeng",d3));
m_user.insert(pair<string,userdevice> ("zhangsanfeng",d4));

Range range=m_user.equal_range("zhangsanfeng");
cout<<range.first->first<<' ';
cout<<range.first->second.m_devicename<<endl;
cout<<range.second->first<<' ';
cout<<range.second->second.m_devicename<<endl;
for(CIT i=range.first;i!=range.second;i++)
{
   cout<<i->second.m_deviced<<','
   <<i->second.m_devicename.c_str()<<','
   <<i->second.m_devicePopedom<<endl;
}
cout<<endl;
}

在linux下这个程序出现的了段错误,已经找到在哪里dump掉了,第48行,cout<<range.second->first<<' ';就是这句,但是不知道为什么会出错啊,求高手指点啊
Linux C

------解决方案--------------------
range.second是否有效?越界了没?
------解决方案--------------------
equal_range 返回的的pair  first指向第一个实例 second指向最后一个实例的下一个 所以range.second 是已经越界了 for(CIT i=range.first;i!=range.second;i++) {    cout<<i->second.m_deviced<<','   <<i->second.m_devicename.c_str()<<','   <<i->second.m_devicePopedom<<endl; } 这种才是正确的访问方式