multiset unique values count

Discussion in 'C++' started by dushkin, Feb 4, 2010.

  1. dushkin

    dushkin Guest

    dushkin, Feb 4, 2010
    #1
    1. Advertising

  2. dushkin

    tonydee Guest

    On Feb 4, 5:09 pm, dushkin <> wrote:
    > Is it possible to know (without iterating ofcourse) how many
    > unique keys are in a multiset?
    > I know that a set has unique values, but I would prefer
    > using multiset in my solution if I have the unique keys
    > counting solution.
    >
    > Example:
    >
    > Given a multiset with values: {1,1,2,4,5,4,3,1} - The unique
    > counting will give me 5 for {1,2,3,4,5} keys.


    > A indirect solution may be adding the multiset items to a
    > set and then get the set size..
    > But I wonder if there is a direct way of getting the unique
    > items number


    STL's multisets do not have any special support for this, i.e. they
    don't spend time / use extra memory to track the unique keys while the
    values are being inserted. Therefore, you must either proactively
    maintain your own count while inserting and erasing from the multiset
    (e.g. insert: check if the multiset includes the key, add 1 to
    unique_keys counter if not). Otherwise, you will need to step through
    the container to count unique keys... using upper_bound() will skip to
    the next key, which just might be faster than a simple iteration, but
    could also be slower (may depend on average number of times a key
    repeats).

    Cheers,
    Tony
     
    tonydee, Feb 9, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-1?q?Jo=E3o_Correia?=

    Order items within multiset AND Find object on multiset

    =?iso-8859-1?q?Jo=E3o_Correia?=, Feb 26, 2007, in forum: C++
    Replies:
    2
    Views:
    480
  2. ToshiBoy
    Replies:
    6
    Views:
    868
    ToshiBoy
    Aug 12, 2008
  3. dushkin
    Replies:
    1
    Views:
    371
    dushkin
    Feb 2, 2010
  4. count unique records

    , Dec 8, 2004, in forum: Ruby
    Replies:
    2
    Views:
    124
    Brian Schröder
    Dec 8, 2004
  5. Token Type
    Replies:
    9
    Views:
    374
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page