how to 'group' an array by STL ?

W

William

for example, there is an array: int a[]={2, 3, 1,3, 2,1};
i want to group it as: {2,2,3,3,1,1} , not care the order.

i can use std::sort( &a[0], &a[len-1]);
but this will also sort the array which will get less performance than
group.

how to do this by STL ? thanks
 
O

Ondra Holub

William napsal:
for example, there is an array: int a[]={2, 3, 1,3, 2,1};
i want to group it as: {2,2,3,3,1,1} , not care the order.

i can use std::sort( &a[0], &a[len-1]);
but this will also sort the array which will get less performance than
group.

how to do this by STL ? thanks

You can do something like:

int* from = &a[0];
while (from != &a[LEN])
from = std::partition(from + 1, &a[LEN],
std::bind1st(std::equal_to<int>(), *from));

But I do not think you will gain better performance than with std::sort.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top