Comparing an element with a set

Discussion in 'C++' started by Sean Dalton, Jul 6, 2008.

  1. Sean Dalton

    Sean Dalton Guest

    Hello,

    I have a two sets OLDLIST and REMOVE.

    I would like to remove every element in OLDLIST if it is also occuring
    in REMOVE
    and store the remaining elements from OLDLIST into NEWLIST.

    So far, I have read in both lists but I'm struggling comparing the
    elements
    - I have something like

    double oldlist[oldsize];
    double remove[remsize];

    for(i = 0; i < oldsize; i++)
    {

    for(k = 0; k < remsize; k ++)

    if(oldlist != remove[k])
    {
    cout<<oldlist[k]<<endl;

    }

    }

    Obviously this is not working because I need to compare all elements
    from REMOVE *at once* with oldlist. So how do I compare all
    elements
    at once?

    Many thanks,
    Sean
     
    Sean Dalton, Jul 6, 2008
    #1
    1. Advertising

  2. Sean Dalton

    Ivan Novick Guest

    On Jul 5, 4:43 pm, Sean Dalton <> wrote:
    > Hello,
    >
    > I have a two sets OLDLIST and REMOVE.
    >
    > I would like to remove every element in OLDLIST if it is also occuring
    > in REMOVE
    > and store the remaining elements from OLDLIST into NEWLIST.
    >
    > So far, I have read in both lists but I'm struggling comparing the
    > elements
    > - I have something like
    >
    > double oldlist[oldsize];
    > double remove[remsize];
    >
    > for(i = 0; i < oldsize; i++)
    > {
    >
    > for(k = 0; k < remsize; k ++)
    >
    > if(oldlist != remove[k])
    > {
    > cout<<oldlist[k]<<endl;
    >
    > }
    >
    > }


    Are you sure you want to use arrays to store your data? This would be
    a n-squared algorithm. Better would be to store the remove set into a
    hash_map or some other container which can do a constant time lookup
    to see if you need to remove the data.

    Ivan Novick
    http://www.mycppquiz.com
     
    Ivan Novick, Jul 6, 2008
    #2
    1. Advertising

  3. Sean Dalton

    James Kanze Guest

    On Jul 6, 2:25 am, Ivan Novick <> wrote:
    > On Jul 5, 4:43 pm, Sean Dalton <> wrote:
    > > I have a two sets OLDLIST and REMOVE.


    > > I would like to remove every element in OLDLIST if it is
    > > also occuring in REMOVE and store the remaining elements
    > > from OLDLIST into NEWLIST.


    > > So far, I have read in both lists but I'm struggling
    > > comparing the elements
    > > - I have something like


    > > double oldlist[oldsize];
    > > double remove[remsize];


    > > for(i = 0; i < oldsize; i++)
    > > {
    > > for(k = 0; k < remsize; k ++)
    > > if(oldlist != remove[k])
    > > {
    > > cout<<oldlist[k]<<endl;
    > > }
    > > }


    > Are you sure you want to use arrays to store your data?


    Since the sizes are changing, he obviously needs std::vector,
    and not C style arrays.

    > This would be a n-squared algorithm.


    Not if the vectors are sorted first. If both vectors are
    sorted, it's O(n) and if only only the remove list is sorted,
    it's O(n ln m) (where n is the number of elements in the old
    vector, and m the number of elements in the remove list).

    > Better would be to store the remove set into a hash_map or
    > some other container which can do a constant time lookup to
    > see if you need to remove the data.


    If you can find a good hash function for double, let us know.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 6, 2008
    #3
    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. MSNews
    Replies:
    1
    Views:
    2,773
    Alvin Bruney [MVP]
    Apr 22, 2004
  2. Rod  Snyder

    moving from form element to form element

    Rod Snyder, May 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    417
    Vidar Petursson
    May 29, 2004
  3. =?Utf-8?B?Um9iZXJ0?=
    Replies:
    3
    Views:
    6,927
    Steven Cheng[MSFT]
    Nov 15, 2005
  4. HANM
    Replies:
    2
    Views:
    751
    Joseph Kesselman
    Jan 29, 2008
  5. ted benedict
    Replies:
    3
    Views:
    156
Loading...

Share This Page