Unique elements problem

G

gouqizi.lvcha

Hi all

I have a large vector with float point numbers in it, for example
(1.1, 2.1 , 3.2 , 3.3 , 4, 6, 3.2, 8). Is there an easy way to
determine
how many uique elements in the array?

Thanks

Rick
 
R

Rapscallion

I have a large vector with float point numbers in it, for example
(1.1, 2.1 , 3.2 , 3.3 , 4, 6, 3.2, 8). Is there an easy way to
determine how many uique elements in the array?

1. copy vector(*)
2. std::sort()
3. std::unique

(*) you must copy the vector because std::unique deletes/overwrites
elements.
 
V

Victor Bazarov

I have a large vector with float point numbers in it, for example
(1.1, 2.1 , 3.2 , 3.3 , 4, 6, 3.2, 8). Is there an easy way to
determine
how many uique elements in the array?

Have you looked at 'std::unique'? It's mutating, so you might want
to make a copy of your array (unless you don't care that it's going
to re-sort them).

V
 
C

Clem.Dickey

Rapscallion said:
1. copy vector(*)
2. std::sort()
3. std::unique

You can also use a set:

#include <set>
#include <iterator>

template <class F>
typename std::iterator_traits<F>::difference_type
count_unique( F first, F last )
{
typedef typename std::iterator_traits<F>::value_type V;
return std::set<V>( first, last ).size();
};

// ...
std::cout << count_unique( v.begin(), v.end() ) << "\n";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top