C++ 从常量ptr*转换为ptr*时出现问题*

C++ 从常量ptr*转换为ptr*时出现问题*,c++,C++,我试图实现一个简单的双向地图数据结构。我发现了一个有用的链接,我决定用第二种方式来实现它,在接受的答案中陈述。我的代码: BiMap.h template <typename X, typename Y> class Comparator { public: bool operator() (const std::pair<X, Y*>& e1, const std::pair<X, Y*>& e2) const; }; templa

我试图实现一个简单的双向地图数据结构。我发现了一个有用的链接,我决定用第二种方式来实现它,在接受的答案中陈述。我的代码:

BiMap.h

template <typename X, typename Y>
class Comparator
{
public:
    bool operator() (const std::pair<X, Y*>& e1, const std::pair<X, Y*>& e2) const;
};

template <typename T1, typename T2>
class BiMap
{
public:
    void insert(const T1& a, const T2& b);
private:
    std::set<std::pair<T1, T2*>, Comparator<T1, T2*>> map1_;
    std::set<std::pair<T2, T1*>, Comparator<T2, T1*>> map2_;
};
模板
类比较器
{
公众:
bool操作符()(const std::pair&e1,const std::pair&e2)const;
};
模板
类BiMap
{
公众:
无效插入(常数T1&a、常数T2&b);
私人:
std::set map1;
std::set map2;
};
BiMap.cpp

template <typename X, typename Y>
bool Comparator<X, Y>::operator() (const std::pair<X, Y*>& e1, const std::pair<X, Y*>& e2) const
{
    return e1.first < e2.first;
}

template <typename T1, typename T2>
void BiMap<T1, T2>::insert(const T1& t1, const T2& t2)
{
    auto itr1 = map1_.emplace(t1, nullptr).first;
    auto itr2 = map2_.emplace(t2, nullptr).first;
    itr1->second = &(itr2->first);      // ERROR
    itr2->second = &(itr1->first);      // ERROR
}
模板
布尔比较器::运算符()(常数std::pair&e1,常数std::pair&e2)常数
{
返回e1.firstsecond=&(itr2->first);//错误
itr2->second=&(itr1->first);//错误
}
main.cpp

int main()
{
    BiMap<int, string> M;
    M.insert(1, "one");
}
intmain()
{
BiMap M;
M.插入(1,“一”);
}
在编译时,我得到以下错误:

c:\users\lucieon\source\repos\algorithms\trie\trie\suffixtree.inl(17): error C2440: '=': cannot convert from 'const _Ty1 *' to 'const _Ty2'
1>        with
1>        [
1>            _Ty1=std::string
1>        ]
1>        and
1>        [
1>            _Ty2=std::string *
1>        ]
1>c:\users\lucieon\source\repos\algorithms\trie\trie\suffixtree.inl(17): note: Conversion loses qualifiers
1>c:\users\lucieon\source\repos\algorithms\trie\trie\suffixtree.inl(12): note: while compiling class template member function 'void trie::BiMap<int,std::string>::insert(const T1 &,const T2 &)'
1>        with
1>        [
1>            T1=int,
1>            T2=std::string
1>        ]
1>c:\users\lucieon\source\repos\algorithms\trie\trie\trie.cpp(140): note: see reference to function template instantiation 'void trie::BiMap<int,std::string>::insert(const T1 &,const T2 &)' being compiled
1>        with
1>        [
1>            T1=int,
1>            T2=std::string
1>        ]
1>c:\users\lucieon\source\repos\algorithms\trie\trie\trie.cpp(139): note: see reference to class template instantiation 'trie::BiMap<int,std::string>' being compiled
1>c:\users\lucieon\source\repos\algorithms\trie\trie\suffixtree.inl(18): error C2440: '=': cannot convert from 'const _Ty1 *' to 'const _Ty2'
1>        with
1>        [
1>            _Ty1=int
1>        ]
1>        and
1>        [
1>            _Ty2=int *
1>        ]
1>c:\users\lucieon\source\repos\algorithms\trie\trie\suffixtree.inl(18): note: Conversion loses qualifiers
c:\users\lucieon\source\repos\algorithms\trie\trie\suffextree.inl(17):错误C2440:“=”:无法从“常量”转换为“常量”
1> 与
1>        [
1> _Ty1=std::string
1>        ]
1> 及
1>        [
1> _Ty2=std::string*
1>        ]
1> c:\users\lucieon\source\repos\algorithms\trie\trie\后缀树.inl(17):注意:转换将丢失限定符
1> c:\users\lucieon\source\repos\algorithms\trie\trie\后缀树.inl(12):注意:编译类模板成员函数“void trie::BiMap::insert(const T1&,const T2&)”时
1> 与
1>        [
1> T1=int,
1> T2=std::string
1>        ]
1> c:\users\lucieon\source\repos\algorithms\trie\trie\trie.cpp(140):注意:请参阅正在编译的函数模板实例化“void trie::BiMap::insert(const T1&,const T2&)”的参考
1> 与
1>        [
1> T1=int,
1> T2=std::string
1>        ]
1> c:\users\lucieon\source\repos\algorithms\trie\trie\trie.cpp(139):注意:请参阅正在编译的类模板实例化“trie::BiMap”的参考
1> c:\users\lucieon\source\repos\algorithms\trie\trie\suffextree.inl(18):错误C2440:“=”:无法从“常量”转换为“常量”
1> 与
1>        [
1> _Ty1=int
1>        ]
1> 及
1>        [
1> _Ty2=int*
1>        ]
1> c:\users\lucieon\source\repos\algorithms\trie\trie\后缀树.inl(18):注意:转换将丢失限定符

我知道
itr1->first
itr2->first
分别是
map1
map2
const
键的一部分,这可能就是我得到这个错误的原因,但这是我使这个数据结构工作的唯一方法。有什么简单的解决方法吗?

您不能在事后修改std::set元素,因为它无法再确保唯一性。似乎您想要一个std::map。 现在的问题是你想要什么作为你的钥匙,什么作为你的价值。当您开始使用指针时,问题就出现了,谁拥有这些对象,以及它们是否始终保持在确切的地址

起点可能是:

std::map<T1, const T2*, Comparator<T1, T2*>> map1_;
std::map<T2, const T1*, Comparator<T2, T1*>> map2_;

这是什么std::set map1;?您不能更改
std::set
的元素,也许最好使用
std::map
来存储迭代器而不是指针?@vladfromscow基本上比较器是比较函数,它只在T1上排序,如果我想在int和string之间建立一个双向映射,这对将是
std::pair
@lucieon您还没有理解我的问题。set和comparator之间这一行的逗号是什么意思?@VladfromMoscow这不是将自定义comparator函数传递给set的方式吗?
auto itr1 = map1_.emplace(t1, &t2).first;
auto itr2 = map2_.emplace(t2, &t1).first;