Google Groups Home
Help | Sign in
Comparing an element with a set
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Sean Dalton  
View profile
 More options Jul 5, 7:43 pm
Newsgroups: comp.lang.c++
From: Sean Dalton <sean.dalton...@yahoo.ca>
Date: Sat, 5 Jul 2008 16:43:52 -0700 (PDT)
Local: Sat, Jul 5 2008 7:43 pm
Subject: Comparing an element with a set
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[i] != remove[k])
          {
              cout<<oldlist[k]<<endl;

          }

}

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

Many thanks,
Sean


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ivan Novick  
View profile
 More options Jul 5, 8:25 pm
Newsgroups: comp.lang.c++
From: Ivan Novick <i...@novickmail.com>
Date: Sat, 5 Jul 2008 17:25:14 -0700 (PDT)
Local: Sat, Jul 5 2008 8:25 pm
Subject: Re: Comparing an element with a set
On Jul 5, 4:43 pm, Sean Dalton <sean.dalton...@yahoo.ca> wrote:

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel T.  
View profile
 More options Jul 5, 9:01 pm
Newsgroups: comp.lang.c++
From: "Daniel T." <danie...@earthlink.net>
Date: Sat, 05 Jul 2008 21:01:11 -0400
Local: Sat, Jul 5 2008 9:01 pm
Subject: Re: Comparing an element with a set

Sean Dalton <sean.dalton...@yahoo.ca> 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.

Use set_difference from the standard header, algorithm.
<http://www.sgi.com/tech/stl/set_difference.html>

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Kanze  
View profile
 More options Jul 6, 5:36 am
Newsgroups: comp.lang.c++
From: James Kanze <james.ka...@gmail.com>
Date: Sun, 6 Jul 2008 02:36:48 -0700 (PDT)
Local: Sun, Jul 6 2008 5:36 am
Subject: Re: Comparing an element with a set
On Jul 6, 2:25 am, Ivan Novick <i...@novickmail.com> wrote:

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:james.ka...@gmail.com
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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google