0

Given the following struct:

struct{
// Keys
int key1;
double key2;
type key3;
...

// Variables to increment
double varibleToIncrement;
...
}

What would be the best container to store these records given that I will need to increment the variables of the ones which are already in it (based on all the keys)?

I am currently using a set<> and have set my variableToIncrement to mutable in order to be able to modify it. Would Multi-Index be a more efficient solution?

Thanks in advance! Max

Batmax
  • 253
  • 8
  • 17
  • This is just a particular case of [C++ STL set update is tedious: I can't change an element in place](https://stackoverflow.com/q/2217878/96780). [This answer](https://stackoverflow.com/a/2221314/96780) sums up the two sensible alternatives you have. I'd go for the key/value split and `std::map`, because it looks much cleaner. – Daniel Daranas Jun 12 '17 at 10:17
  • @dasblinkenlight So far, when I create a record with some keys, it is either added to the set (if not already in there) or only the variableToincrement is changed. – Batmax Jun 12 '17 at 10:58
  • @DanielDaranas I will check out this answer, thanks! – Batmax Jun 12 '17 at 10:58
  • @Batmax That shouldn't cause any problems then. If you don't see any efficiency problems with a `set`, don't optimize it prematurely. – Sergey Kalinichenko Jun 12 '17 at 11:01
  • @dasblinkenlight One of my issue is indeed speed... I think that the sorting of that many keys (I sometimes have live 6-7) slow down everything – Batmax Jun 12 '17 at 14:41

1 Answers1

0

One of my issue is indeed speed. I think that the sorting of that many keys slows down everything

Use a hash-based container to avoid sorting. For set use std::unordered_set<T>.

This Q&A explains how to make hash and equality functions for use with hash-based containers.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523