2016-05-10 8 views
0

私はunordered_map::emplace()を呼び出していますが、返された値(ペア)を保存しています。私はちょうどペアから挿入された値にアクセスしたいが、私の人生にとって私はこの混乱するペアの正しい構成を理解することはできない。unordered_map :: emplace戻り値から挿入された要素にアクセスする

マイ順不同マップ定義:私はunordered_map::emplace()documentation見てきた

std::unordered_map<GUID, shared_ptr<Component>> components; 

。これによれば、ペアの最初の要素はshared_ptr<Component>でなければなりませんが、コンパイラは幸せではありません。 Error 2 error C2227: left of '->gUid' must point to class/struct/union/generic type

class Component { 
public: 
    template<typename T, typename... Params> 
    GUID addComponent(Params... params) 
    { 
     auto cmp = Component::create<T>(params...); 
     auto res = components.emplace(cmp->gUid, cmp); 

     if (!res.second) { 
      GUID gUid; 
      getNullGUID(&gUid); 
      return gUid; 
     } 

     return (*res.first)->gUid; // compiler error here 
     // *Yes I know I can do: return cmp->gUid; 
    } 

    GUID gUid; // initialised in constructor 
    std::unordered_map<GUID, std::shared_ptr<Component>> components; 
}; 

任意のアイデアはどのように正しくペア第二の値にアクセスするには:以下のコードで

私はエラーを取得しますか? unordered_mapため、 pair<key, value>へのポインタのように動作し、 -

答えて

1

emplaceから返された一対のfirstイテレータあります。だから、ペアというから値を取得するには、secondを必要とする:

return res.first->second->gUid; 
関連する問題