Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 是否可以使用无序::映射::查找映射';s键是指向字符串的指针?_C++_Pointers_Unordered Map - Fatal编程技术网

C++ 是否可以使用无序::映射::查找映射';s键是指向字符串的指针?

C++ 是否可以使用无序::映射::查找映射';s键是指向字符串的指针?,c++,pointers,unordered-map,C++,Pointers,Unordered Map,我有这张地图: using namespace std; // just for readability unordered_map<shared_ptr<string>, whatever_type> map; 使用循环是唯一的选择吗 另外,我感兴趣的是find方法是否有一些特殊的签名,或者其他方法来避免循环。我能够使它与常规map一起工作。代码如下: #include <map> #include <string> #include <

我有这张地图:

using namespace std; // just for readability

unordered_map<shared_ptr<string>, whatever_type> map;
使用循环是唯一的选择吗


另外,我感兴趣的是
find
方法是否有一些特殊的签名,或者其他方法来避免循环。

我能够使它与常规map一起工作。代码如下:

#include <map>
#include <string>
#include <memory>

struct PtrCompare {
    using is_transparent = bool;
    auto operator() (const std::shared_ptr<std::string>& lhs, const std::shared_ptr<std::string>& rhs) const {
        return *lhs < *rhs;
    }
    auto operator() (const std::shared_ptr<std::string>& lhs, const std::string& rhs) const {
        return *lhs < rhs;
    }
    auto operator() (const std::string& lhs, const std::shared_ptr<std::string>& rhs) const {
        return lhs < *rhs;
    }
};

std::map<std::shared_ptr<std::string>, int, PtrCompare>  my_hash;

auto check(const std::string& x) {
    return my_hash.find(x);
}
#包括
#包括
#包括
结构PtrCompare{
使用is_transparent=bool;
自动运算符(){
返回*左侧<*右侧;
}
自动运算符(){
返回*lhs
它使用C++14上提供的透明比较器特性。然而,无序映射的透明比较器仅在C++20中可用,因此,我的解决方案不适用于无序映射


使用非透明比较器处理无序映射的方法是仍然使用自定义哈希器和比较器,这将在取消引用参数后比较参数,但从要比较的字符串创建一个共享指针,并使用此共享指针查找。对我来说,这个解决方案非常难看,所以我不是建议这样做。

我能够使它与常规地图一起工作。代码如下:

#include <map>
#include <string>
#include <memory>

struct PtrCompare {
    using is_transparent = bool;
    auto operator() (const std::shared_ptr<std::string>& lhs, const std::shared_ptr<std::string>& rhs) const {
        return *lhs < *rhs;
    }
    auto operator() (const std::shared_ptr<std::string>& lhs, const std::string& rhs) const {
        return *lhs < rhs;
    }
    auto operator() (const std::string& lhs, const std::shared_ptr<std::string>& rhs) const {
        return lhs < *rhs;
    }
};

std::map<std::shared_ptr<std::string>, int, PtrCompare>  my_hash;

auto check(const std::string& x) {
    return my_hash.find(x);
}
#包括
#包括
#包括
结构PtrCompare{
使用is_transparent=bool;
自动运算符(){
返回*左侧<*右侧;
}
自动运算符(){
返回*lhs
它使用C++14上提供的透明比较器特性。然而,无序映射的透明比较器仅在C++20中可用,因此,我的解决方案不适用于无序映射


使用非透明比较器处理无序映射的方法是仍然使用自定义哈希器和比较器,这将在取消引用参数后比较参数,但从要比较的字符串创建一个共享指针,并使用此共享指针查找。对我来说,这个解决方案非常难看,所以我不是建议这样做。

我强烈建议不要使用
共享\u ptr
作为密钥。我无法想象为什么它可能是可取的。@SergeyA如果字符串非常大,并且它们也存储在其他位置怎么办?您可以使用自定义hashcode和比较器进行
无序映射。我看不出其他方法能让这件事快速发生。我从来没有这样做过,但可能您必须为键定义自己的包装类。@SergeyA有道理,但希望最后消失:-)这就是为什么我问您可能必须使用自定义哈希函数和自定义比较函数来解引用指针。您可能希望使用透明的分配器。共享指针也让它变得非常奇怪,你能改变设计使用普通的非拥有指针吗?我强烈建议不要使用
shared\u ptr
作为键。我无法想象为什么它可能是可取的。@SergeyA如果字符串非常大,并且它们也存储在其他位置怎么办?您可以使用自定义hashcode和比较器进行
无序映射。我看不出其他方法能让这件事快速发生。我从来没有这样做过,但可能您必须为键定义自己的包装类。@SergeyA有道理,但希望最后消失:-)这就是为什么我问您可能必须使用自定义哈希函数和自定义比较函数来解引用指针。您可能希望使用透明的分配器。共享指针也让它变得非常奇怪,你能改变设计使用普通的非拥有指针吗?你是说透明的比较器吗?哇。我对C++的知识很低,我还不能理解这个解决方案(以及它的性能特征)。但是,谢谢你!我希望我能很快提高我的知识水平,以便理解并很快使用它。你是说透明比较器吗?哇。我对C++的知识很低,我还不能理解这个解决方案(以及它的性能特征)。但是,谢谢你!我希望我能很快提高我的知识,以便理解并可能很快使用它。