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

后端 未结 5 1207
不知归路
不知归路 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:32

    It seems like you really want a std::set or std::list of keys, and then iterate over that set later to create your map.

    If for some reason this isn't acceptable, you could wrap your float with some type of object that implements operator float(), and various conversion functions for float, but also has a "good()" flag or somesuch.

    If your final values are actually floats, you could initialize the values to +/- NaN as a simple placeholder.

    I would say boost::optional is a better option for production code, but it depends on your requirements.

提交回复
热议问题