Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > number of distinct elements in a const container and iterating over the distinct elements

Reply
Thread Tools

number of distinct elements in a const container and iterating over the distinct elements

 
 
Hicham Mouline
Guest
Posts: n/a
 
      04-11-2010
Hello,
I have posted this question elsewhere:
>Given a std::vector<T> and a Compare<T> comparator, how can I:
>
> 1. determine the number of different elements in the vector?
> 2. get iterators to the list of distinct elements in the vector?


The answer given was:

>With 2 iterators b and e,
> std::sort(b, e);
> m = std::unique(b, e, Compare<T>());
>Then you have the std::distance(b, m) unique elements available in the
>range [b, m).


My question was incomplete.
1. I wish to keep the container const and it may be unsorted.
2. I wish to avoid making a copy if possible and would accept a slower
algorithm if there is one.
3. Is it possible to then iterate (possibly by writing a new iterator) over
the distinct elements?

regards,


 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      04-11-2010
Hicham Mouline wrote:

> Hello,
> I have posted this question elsewhere:
>>Given a std::vector<T> and a Compare<T> comparator, how can I:
>>
>> 1. determine the number of different elements in the vector?
>> 2. get iterators to the list of distinct elements in the vector?

>
> The answer given was:
>
>>With 2 iterators b and e,
>> std::sort(b, e);
>> m = std::unique(b, e, Compare<T>());
>>Then you have the std::distance(b, m) unique elements available in the
>>range [b, m).

>
> My question was incomplete.
> 1. I wish to keep the container const and it may be unsorted.
> 2. I wish to avoid making a copy if possible and would accept a slower
> algorithm if there is one.
> 3. Is it possible to then iterate (possibly by writing a new iterator)
> over the distinct elements?


Sure, with almost the same algorithm as above. You need to introduce yet
another level of indirection. Use a custom predicate like:

class ForwardCompare {

typedef std::vector<T> T_vector;

T_vector * reference; // point to the original vector

bool operator() ( T_vector::size_type i, T_vector::size_type j ) const {
return( Compare<T>()( reference->at(i), reference->at(j) ) );
}

};

Now, you create a vector of indices from 0 to size-1, sort that vector using
ForwardCompare, make it unique using ForwardCompare and what you end up with
is a vector containing indices to pairwise Compare<T>-different elements of
the original vector.


Best

Kai-Uwe Bux

 
Reply With Quote
 
 
 
Reply

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Iterating over dict and removing some elements Ulrich Eckhardt Python 6 05-12-2010 07:28 AM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
subject: xpath select distinct over 2 elements will XML 1 08-15-2007 10:32 AM
XSLT: iterating all child elements and accessing homonymous childrenin sibling elements Gerald Aichholzer XML 2 06-27-2006 03:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57