在一个项目中用多线程访问全局变量,全局变量是用STL MAP声明的,程序运行时有时却老有迭代器的异常,要么不兼容,要么引用不到了,索性自己写了一个,虽然不是真正的MAP类,我是用链表实现的,可是用起来时也遇到一些问题,链表长度为n,中间某个节点的pNext为空了,这样就麻烦了我自己找不出原因大家看我这实现有啥问题/*
此类实现中key目前只支持STL的string类型 value值类型不限
*/
template<class Key,class Value>
class CMapTemplate
{
public:
//map实际上也是基于链表实现,MapT结构即为链表节点的结构
struct mapT
{
//指向上一个节点
mapT * pPrevious;
//map的关键字
Key key;
//关键字对应的值
Value value;
//指向下一个节点
mapT * pNext;
}; CMapTemplate()
{
length=0;
//map初始化时new一个mapT节点用于存放首个加入的节点的指针 该指针村在pNext字段内
pHead = new mapT();
if (NULL!=pHead)
{
memset(pHead,0,sizeof(mapT));
}
//初始时 pTail为空
pTail = NULL;
}; ~CMapTemplate()
{
}; mapT * insert(Key& x,Value &y)
{
//pHead为链表头 空链表也有 只不过空链表pTail为空,若pTail为空则new一个 存入数据
if (NULL==pTail)
{
pTail = new mapT();
if (NULL!=pTail)
{
//如果头不为空 则pTail连载pHead之后
if (NULL!=pHead)
{
pTail->key=x;
pTail->value=y;
pTail->pNext=NULL;
pTail->pPrevious=pHead;
pHead->pNext=pTail;
length++;
return pTail;
}
else
{
//若头为空 则pTail取代pHead
pTail->key=x;
pTail->value=y;
pTail->pNext=NULL;
pTail->pPrevious=NULL;
pHead=pTail;
pTail = NULL;
length++;
return pHead;
}
}
else
{
//怎么分配不到内存呢?
return NULL;
}
}
else
{
//此时pHead后已经有了节点 即实际存放了内容 则新new一个节点 连载pTail之后 并取代pTail节点
mapT * li = new mapT();
if (NULL!=li)
{
li->key=x;
li->value=y;
li->pNext=NULL;
li->pPrevious=pTail;
pTail->pNext=li;
pTail=li;
length++;
return li;
}
else
{
return NULL;
}
}
}; int getLength()
{
return length;
}; //如果T是一个结构体 就用这个函数
mapT * erase(mapT *pElement)
{
if (NULL!=pElement)
{
//如果是map头 则取头之后的下一个节点 取代pHead
if (pElement==pHead)
{
pHead=pElement->pNext; if (length>0&&pHead->pNext==NULL)
{
::MessageBox(NULL,"问题所在","",0);
}
delete pElement;
pElement=NULL;
//::MessageBox(NULL,"不能删除链表头","",0);
return pHead;
} //如果是头尾之外的节点 则连接相应指针后删除
if (pElement!=pHead&&pElement!=pTail)
{
mapT * pTemp=NULL;
pTemp = pElement;
if (NULL!=pElement->pPrevious)
{
pElement->pPrevious->pNext=pElement->pNext;
}
if (NULL!=pElement->pNext)
{
pElement->pNext->pPrevious=pElement->pPrevious;
}
pElement=pElement->pNext;
--length;
if (length>0&&pHead->pNext==NULL)
{
::MessageBox(NULL,"问题所在","",0);
}
delete pTemp;
pTemp = NULL;
return pElement;
} //如果是尾节点 则删掉节点 赴pTail为空
if (pElement==pTail)
{
if (NULL!=pElement->pPrevious)
{
pElement->pPrevious->pNext=NULL;
}
--length;
if (length>0&&pHead->pNext==NULL)
{
::MessageBox(NULL,"问题所在","",0);
}
pTail = NULL;
delete pElement;
pElement = NULL;
return pTail;
}
}
else
{
::MessageBox(NULL,"不能删除空元素","",0);
}
return NULL;
}
public:
mapT *pHead;
mapT *pTail;
int length;
};
此类实现中key目前只支持STL的string类型 value值类型不限
*/
template<class Key,class Value>
class CMapTemplate
{
public:
//map实际上也是基于链表实现,MapT结构即为链表节点的结构
struct mapT
{
//指向上一个节点
mapT * pPrevious;
//map的关键字
Key key;
//关键字对应的值
Value value;
//指向下一个节点
mapT * pNext;
}; CMapTemplate()
{
length=0;
//map初始化时new一个mapT节点用于存放首个加入的节点的指针 该指针村在pNext字段内
pHead = new mapT();
if (NULL!=pHead)
{
memset(pHead,0,sizeof(mapT));
}
//初始时 pTail为空
pTail = NULL;
}; ~CMapTemplate()
{
}; mapT * insert(Key& x,Value &y)
{
//pHead为链表头 空链表也有 只不过空链表pTail为空,若pTail为空则new一个 存入数据
if (NULL==pTail)
{
pTail = new mapT();
if (NULL!=pTail)
{
//如果头不为空 则pTail连载pHead之后
if (NULL!=pHead)
{
pTail->key=x;
pTail->value=y;
pTail->pNext=NULL;
pTail->pPrevious=pHead;
pHead->pNext=pTail;
length++;
return pTail;
}
else
{
//若头为空 则pTail取代pHead
pTail->key=x;
pTail->value=y;
pTail->pNext=NULL;
pTail->pPrevious=NULL;
pHead=pTail;
pTail = NULL;
length++;
return pHead;
}
}
else
{
//怎么分配不到内存呢?
return NULL;
}
}
else
{
//此时pHead后已经有了节点 即实际存放了内容 则新new一个节点 连载pTail之后 并取代pTail节点
mapT * li = new mapT();
if (NULL!=li)
{
li->key=x;
li->value=y;
li->pNext=NULL;
li->pPrevious=pTail;
pTail->pNext=li;
pTail=li;
length++;
return li;
}
else
{
return NULL;
}
}
}; int getLength()
{
return length;
}; //如果T是一个结构体 就用这个函数
mapT * erase(mapT *pElement)
{
if (NULL!=pElement)
{
//如果是map头 则取头之后的下一个节点 取代pHead
if (pElement==pHead)
{
pHead=pElement->pNext; if (length>0&&pHead->pNext==NULL)
{
::MessageBox(NULL,"问题所在","",0);
}
delete pElement;
pElement=NULL;
//::MessageBox(NULL,"不能删除链表头","",0);
return pHead;
} //如果是头尾之外的节点 则连接相应指针后删除
if (pElement!=pHead&&pElement!=pTail)
{
mapT * pTemp=NULL;
pTemp = pElement;
if (NULL!=pElement->pPrevious)
{
pElement->pPrevious->pNext=pElement->pNext;
}
if (NULL!=pElement->pNext)
{
pElement->pNext->pPrevious=pElement->pPrevious;
}
pElement=pElement->pNext;
--length;
if (length>0&&pHead->pNext==NULL)
{
::MessageBox(NULL,"问题所在","",0);
}
delete pTemp;
pTemp = NULL;
return pElement;
} //如果是尾节点 则删掉节点 赴pTail为空
if (pElement==pTail)
{
if (NULL!=pElement->pPrevious)
{
pElement->pPrevious->pNext=NULL;
}
--length;
if (length>0&&pHead->pNext==NULL)
{
::MessageBox(NULL,"问题所在","",0);
}
pTail = NULL;
delete pElement;
pElement = NULL;
return pTail;
}
}
else
{
::MessageBox(NULL,"不能删除空元素","",0);
}
return NULL;
}
public:
mapT *pHead;
mapT *pTail;
int length;
};
解决方案 »
- 求ghost 远程监控,学习资料
- 下拉列表添加选项导致系统运行错误
- 帮忙多线程问题
- createfile如何能注入kernel32.dll实现对文件的打开监控,我这里的createfile为什么不好使。
- 如何从bits数据得到HBITMAP?
- Doc/View中CListView中编辑不对问题
- 在VS2005 上编辑VC++6 源码 #import ...dll rename_namespace("CFX6") generate cs2039 error
- 请教
- 请问一下!当我浏览dudu,com时候,就会自动下载并且安装一个dudu下载加速器?并且没有数字签名安全警告!他这个是怎么做到的?
- 操作系统执行时,是否会记住每块内存的类型(1)
- 求SQL或存储过程
- 关于WININET超时,急~
出现异常那是lz没有做好同步导致