首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

multi地图有关问题

2012-12-20 
multimap问题自己写了一个类实现multimap的封装类大体结构如下:#include boost/any.hpp#include string

multimap问题
自己写了一个类实现multimap的封装
类大体结构如下:
#include <boost/any.hpp>
#include <string>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

class CDynamicDict {
  typedef boost::any OBJECT_ANY;
typedef multimap<string, OBJECT_ANY> DICT_MAP;
typedef DICT_MAP::const_iterator const_iterator;
typedef DICT_MAP::iterator iterator;

public:
    CDynamicDict();
    CDynamicDict(const CDynamicDict& orig);
    virtual ~CDynamicDict();

    /**
     * 设置对象
     * @param name
     * @param object
     */
    void setValueByName(string name, OBJECT_ANY *object) {
       // removetValueByName(name,false);

        valueMap->insert(DICT_MAP::value_type(name, object));
    }

    /*void removetValueByName(string name, bool isDelete) {
        OBJECT_ANY findObejct = getValueByName(name);
        if (findObejct != NULL && isDelete) {
            delete findObejct;
        }
        
        valueMap->erase(name);
    }*/

    /**
     * 获得对象
     * @param name
     * @return 
     */
iterator getValueByName(string name)
{
        DICT_MAP::iterator it= valueMap->find(name);
        if (it == valueMap->end())
            return NULL;
 return it;
}

iterator upper_bound(string name)
{
DICT_MAP::iterator it=valueMap->upper_bound(name);
if(it == valueMap->end())
return NULL;
return it;
}
iterator lower_bound(string name)
{
DICT_MAP::iterator it=valueMap->lower_bound(name);
if(it == valueMap->end())
return NULL;
return it;
}
iterator end()
{
DICT_MAP::iterator it=valueMap->end();
return it;
}

void addValueByName(string name, boost::any &object) {
DICT_MAP::value_type my_pair(name, object);
       iterator it;
         it= valueMap->insert(my_pair);
    }

    unsigned long getCountByName(string name) {
        return this->valueMap->size();        
    }
    
    void setBOMode(bool BOMode);
    
    bool isBOMode() const;


string print_str(boost::any& arr)
{
if(typeid(string)==arr.type())
{
return boost::any_cast<string>(arr);
}
else
{
return NULL;
}
}
int print_int(boost::any& arr)
{
if(typeid(int)==arr.type())
{
return boost::any_cast<int>(arr);
}
else
{
return -1;
}
}
double print_double(boost::any& arr)
{
if(typeid(double)==arr.type())
{
return boost::any_cast<double>(arr);
}
else
{
return -1;
}
}

private:
    bool BOMode;
    DICT_MAP *valueMap;

};
CDynamicDict::CDynamicDict() {
    BOMode=false;
    valueMap=new DICT_MAP();
}



void main()
{
CDynamicDict out_map;
boost::any p=string("l");
boost::any p1=string("2");
out_map.addValueByName("mm",p);
out_map.addValueByName("mm",p1);
multimap<string,boost::any>::iterator  it=out_map.lower_bound("mm");
multimap<string,boost::any>::iterator  it_=out_map.upper_bound("mm");

//while((it->first!=NULL)&&(it->first=="mm"))//这行出现问题,直接程序蹦了,貌似迭代器无法比较了,不知道为什么?
{
string str=boost::any_cast<string>(it->second);
cout << str << "\t" << endl;
++it;
}

}

问题如下,我迭代
[解决办法]
自己定义的迭代器不能进行比较操作,如while循环里面的操作,不知道为什么,求高手指导,不吝赐教,急!

热点排行