Boost logo

Boost Users :

Subject: Re: [Boost-users] Distinct elements in a std::vector<T>
From: Scott McMurray (me22.ca+boost_at_[hidden])
Date: 2010-04-10 17:40:37


On 10 April 2010 17:22, Hicham Mouline <hicham_at_[hidden]> wrote:
>
> 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?
>

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).

(Note that as a generic C++ question, this boost-specific list is
perhaps not the best place to ask. You might consider something like
Freenode's ##C++ IRC channel instead, where the medium means you would
likely get a faster response as well, as the question doesn't require
investigation or bug-hunting.)


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net