Std :: multiset <int> vs. std :: map <int, std :: size_t> to store multiple repeatable integer values

I want to create a record in which information about

  • a) which elements are present and
  • b) the number of elements of each species present

in the tree node. I would explicitly save this information only for leaf nodes, while information for the parent node can be obtained by combining the information of all its children (for example, child 1 has 3 objects A, 1 object B, child 2 has 1 object A, 2 objects C - of the parent object have 4 objects A, 1 object B and 2 from C).

I will be careful when requesting this information from the parent nodes so as not to request, use and delete information for the child node and then for its parent node, but the upstream construct will be a common operation. The other two general operations are directly derived from what I store: is an object of genus X real? and how many objects of type X are present? and also how many kinds of objects are present?

The types of objects are represented as integers, and the numbers of objects are always integers. What is the best choice (and arguments for the selected choice):

  • use std::multiset<int>and work with operations std::multiset::count()and std::multiset::find()(easier to combine, but duplication of elements, the total number of elements that are difficult to obtain)
  • std::map<int, std::size_t> ( , std::map::find() , , )

!

+5
2

n k , std::multiset n (*). std::map k ( ) .

std::multiset, , , , . , multiset n , map k .

, , std::map. " " size, .

, " ", , operator[] . find .

(*) ++ , () BST, , , .

+7

std::vector<int>? , , :

  • X ? std::binary_search
  • X ? std::equal_range, .first .second
  • ?
    • std::unique_copy, size() , ...
    • , std::binary_search ​​

- ( ) . , , . , , , , .

- . std::map, , , , .. .

+2

All Articles