Suppose I have a struct called "ClientInfo", This struct holds some information about some clinet. And suppose each client has a constant ID and some other information like 'Address' or 'Balance' that can be changed. If I want to store clients information in some container that gives me log(n) insertion/deletion then I think std::set is the most suitable one. But unfortunetly set doesn't allow any item inside it to be modified even if the modification won't affect the order (I think). I've looked on the internet for solutions and all I found was one of the following:
- Use 'mutable'
- Use const_cast
- You can erase and insert again (O(1))
- You can use std::map
--
1/2 - I don't think I'm gonna use the first two options because I don't wanna mess with the const-correctness of my program.
3 - Also I think even though erasing and inserting is O(1) I think if I can just modify the element, That would be faster.
4 - Also I don't think std::map is the right soultion since I want to have access to the clients' ID. If I use std::map, I have two choises:
- To copy the ID twice, Once for the key of the map, And once inside the struct.
- To take off the ID from the struct to avoid duplication, But in this case, I won't have a direct access to the ID of the client inside the struct.
It's obvious that I don't wanna copy the ID since it can be something expensive to copy, Such as std::string or some object.
--
So my question is: How to modify an element inside std::set directly without using 'mutable' or 'const_cast', Without erasing and inserting again and without using std::map? How to tell std::set that the ID is constant and that it's the only thing that affects the order of the elements and tell it to allow modifying anything else other than the ID?