Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8027259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:45:22+00:00 2026-06-04T23:45:22+00:00

I want to create a record that would hold the information about a) what

  • 0

I want to create a record that would hold the information about

  • a) what kind of elements are present and
  • b) the number of elements of each kind present

in a node of a tree. I would explicitly store this information only for the leaf nodes, while the information for the parent node can be obtaining through combining the information of all of it’s children (e.g. child 1 has 3 objects of A, 1 object of B, child 2 has 1 object of A, 2 objects of C — parent has 4 objects of A, 1 object of B and 2 of C).

I will be careful when requesting this information from the parent nodes not to first request, use and discard information for a child node and then for its parent node, but the upward construction will be a common operation. Other two common operations are directly derived from what I store: is the object of kind X present? and how many objects of kind X is present? and also how many kinds of objects are present?

Object kinds are represented as integers, and the object numbers are always integer values. What is the better choice (and arguments for the selected choice):

  • use std::multiset<int>, and operate with std::multiset::count() and std::multiset::find() operations (easier union but duplication of elements, total distinct element count hard to obtain)
  • use std::map<int, std::size_t> with the kind as a key and number of objects as a value (no duplicate elements, std::map::find() function present, size gives the correct number of object kinds stored, but accessing a non-existent element increases the size unintentionally)

Thank you for your suggestions!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:45:24+00:00Added an answer on June 4, 2026 at 11:45 pm

    To store a total of n items with k distinct values per your comparison predicate, an std::multiset allocates n binary search tree nodes(*). An std::map allocates only k (slightly larger) nodes.

    You’d use std::multiset when two items can be considered equal by your comparison predicate, but must still be explicitly stored, because they differ in some aspect that the comparison predicate does not check. Also, iterating over a multiset generates each of the n items, whereas a map would generate each of the k distinct items with the count for each.

    In the case where the items are just integers, go with std::map. Your “how many distinct items” query would then just be a call to size, which runs in constant time.

    Your claim that “accessing a non-existent element increases the size unintentionally” is only true if you use operator[] to access nodes. find does not exhibit this behavior.

    (*) The C++ standard does not guarantee that these containers are implemented as (balanced) BSTs, but in all implementations that I’ve seen, they are.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would want to create an array that will hold records retrieved from a
MYSQL/PHP, I want to create a record of activities that people perform on the
i want to create a NFC SmartPoster which dial a number with Action Record
i want create a counter that retrieve the number of month, day, hour, minute
I want to create a input dialog box when new record is created and
I want to create a table in MS SQL Server 2005 to record details
I want to create a before delete trigger. When I delete a record from
I want create an array in JSF EL. How can I do that? Is
I want to be able to create a top-level Team site. On that site,
What would be the generic way to create record title and slub based on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.