私はキーとしてカスタムオブジェクトへのポインタを使用する順序のないマップを持っています。 何らかの理由で、キーで値を参照することは、キーがconstでない場合にのみ機能します。ここで無秩序なマップ - constキーで値を取得
は(スタンドインカスタムオブジェクト用としてstd::string
で)の例である:
std::unordered_map<std::string*, int> my_map;
std::string key {"test"};
const std::string const_key {"test2"};
auto value = my_map.at(&key); // this works as expected
auto other_value = my_map.at(&const_key); // this doesn't compile
error: invalid conversion from 'const string* {aka const std::__cxx11::basic_string<char>*}'
to 'std::unordered_map<std::__cxx11::basic_string<char>*, int>::key_type
{aka std::__cxx11::basic_string<char>*}' [-fpermissive]
はなぜルックアップが非constのようにポインタを必要とするでしょうか?
'std :: unordered_map(const std :: string *、int> my_map;'? –
)を使用するとうまくいきますが、私のユースケースではキーにアクセスすることはできません。 – BoshWash
それはあなたがあなたのマップを宣言した方法なのですか? –