How to add valid key without specifying value to a std::map?

后端 未结 5 1204
不知归路
不知归路 2021-02-20 05:10

I have a std::map, and I would like to add a valid key to iterate over it later, but without giving any value (it will be given later on in the course of the iterations).

5条回答
  •  星月不相逢
    2021-02-20 05:24

    I suppose something that could help you out is Boost.Optional.

    #include 
    #include 
    
    class CantConstructMe
    {
        CantConstructMe() {}
    };
    
    int main()
    {
        std::map > m;
        m[0];
    }
    

    The lack of available default constructor is not an issue, by default optional will be empty.

提交回复
热议问题