Is using a map where value is std::shared_ptr a go

2019-06-20 05:44发布

problem is simple: We have a class that has members a,b,c,d... We want to be able to quickly search(key being value of one member) and update class list with new value by providing current value for a or b or c ... I thought about having a bunch of
std::map<decltype(MyClass.a/*b,c,d*/),shared_ptr<MyClass>>.

1) Is that a good idea?

2) Is boost multi index superior to this handcrafted solution in every way?

PS SQL is out of the question for simplicity/perf reasons.

2条回答
Explosion°爆炸
2楼-- · 2019-06-20 06:24

You want want to consider containing all of your maps in a single class, arbitrarily deciding on one of the containers as the one that stores the "real" objects, and then just use a std::map with a mapped type of raw pointers to elements of the first std::map.

This would be a little more difficult if you ever need to make copies of those maps, however.

查看更多
家丑人穷心不美
3楼-- · 2019-06-20 06:30
  1. Boost MultiIndex may have a distinct disadvantage that it will attempt to keep all indices up to date after each mutation of the collection. This may be a large performance penalty if you have a data load phase with many separate writes.

  2. The usage patterns of Boost Multi Index may not fit with the coding style (and taste...) of the project (members). This should be a minor disadvantage, but I thought I'd mention it

  3. As ildjarn mentioned, Boost MI doesn't support move semantics as of yet

Otherwise, I'd consider Boost MultiIndex superior in most occasions, since you'd be unlikely to reach the amount of testing it received.

查看更多
登录 后发表回答