Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to 'group' an array by STL ?

Reply
Thread Tools

how to 'group' an array by STL ?

 
 
William
Guest
Posts: n/a
 
      01-18-2007

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

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      01-18-2007
On Jan 18, 10:46 am, William <(E-Mail Removed)> 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
http://www.cppreference.com/cppalgorithm/partition.html for more info.

--
Erik Wikström

 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      01-18-2007

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:artition(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.

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
stl questions: how can I compare 2 stl list? silverburgh.meryl@gmail.com C++ 5 04-16-2006 09:57 PM
a stl map which use stl pair as the key Allerdyce.John@gmail.com C++ 2 02-22-2006 07:25 AM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
To STL or not to STL Allan Bruce C++ 41 10-17-2003 08:21 PM



Advertisments