Velocity Reviews > C++ > Best way of comparing two containers?

# Best way of comparing two containers?

Karl Heinz Buchegger
Guest
Posts: n/a

 07-09-2004
Dylan wrote:
>
> On Thu, 08 Jul 2004 18:48:52 -0700, Andrey Tarasevich
> <(E-Mail Removed)> wrote:
>
> >Dylan wrote:
> >> ...
> >> Thanks for your answer, but the reason I stipulated that the elements
> >> can be in any order is that, for the problem I'm working on, it's
> >> unreasonable to assume there is a sorting criteria defined for the
> >> element type (or that one can be defined using the type interface).
> >> ...

> >
> >In that case you should specify what kind of criteria you _do_ have
> >defined. Boolean equality criteria only? Something else?

>
>
> Boolean equality criteria only (==)

Hmm. Would it be possible to make up some artificial 'less'
relationship just for the purpose of sorting? It doesn't
matter if that 'less' actually makes some sense in the
assignment space.
(Such a thing is almost always possible to do)

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)

Ioannis Vranos
Guest
Posts: n/a

 07-09-2004
Dylan wrote:

> I'd like to compare two containers. They should be considered
> equivalent if both containers have the same number of elements with
> the same values, no matter what order the values are in.
>
> For instance the containers
>
> A = [1, 2, 3]
> B = [1, 2, 3]
>
> are obviously equal, but so would be
>
> A = [3, 2, 1]
> B = [2, 1, 3]
>
> as would
>
> A = [2, 2, 5, 1]
> B = [2, 1, 5, 2]
>
> What's the best (quickest) way of comparing containers in this way?

I think you need to use std::set or std::multiset.

Regards,

Ioannis Vranos

Ioannis Vranos
Guest
Posts: n/a

 07-09-2004
Example:

#include <iostream>
#include <set>

int main()
{
using namespace std;

multiset<int>t1, t2;

t1.insert(2);
t1.insert(2);
t1.insert(5);
t1.insert(1);

t2.insert(2);
t2.insert(1);
t2.insert(5);
t2.insert(2);

if(t1==t2)
cout<<"\nEqual!\n";

}

Regards,

Ioannis Vranos

Jeff Flinn
Guest
Posts: n/a

 07-09-2004

"Ioannis Vranos" <(E-Mail Removed)> wrote in message
news:ccmhat\$l13\$(E-Mail Removed)...
> Example:
>
>
> #include <iostream>
> #include <set>
>
>
> int main()
> {
> using namespace std;
>
> multiset<int>t1, t2;
>
> t1.insert(2);
> t1.insert(2);
> t1.insert(5);
> t1.insert(1);
>
> t2.insert(2);
> t2.insert(1);
> t2.insert(5);
> t2.insert(2);
>
> if(t1==t2)
> cout<<"\nEqual!\n";
>
> }

Which works for int, but the OP said his T only implements operator==, and
not any of the inequalities.

Jeff F

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Will Lastname ASP .Net 20 03-29-2011 02:53 PM GenxLogic Java 3 12-06-2006 08:41 PM darrel ASP .Net 2 12-23-2005 05:11 PM =?Utf-8?B?RE9UTkVUR1VZ?= ASP .Net 1 08-24-2005 10:17 AM Steffen Loringer ASP .Net 1 04-26-2004 09:55 AM

Advertisments