Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   how to 'group' an array by STL ? (http://www.velocityreviews.com/forums/t459889-how-to-group-an-array-by-stl.html)

 William 01-18-2007 09:46 AM

how to 'group' an array by STL ?

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

 =?iso-8859-1?q?Erik_Wikstr=F6m?= 01-18-2007 10:17 AM

Re: how to 'group' an array by STL ?

On Jan 18, 10:46 am, William <x...@abc.com> wrote:
> 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.

You probably want the partition algorithm, take a look at

--
Erik Wikström

 Ondra Holub 01-18-2007 10:32 AM

Re: how to 'group' an array by STL ?

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.

 All times are GMT. The time now is 12:17 PM.