By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,412 Members | 996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,412 IT Pros & Developers. It's quick & easy.

Need help with STL (?)

P: n/a
Tom
How can you delete doubles of an element in a vector, all elements
check and then delete the extra ones that are in the list.
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a


Tom wrote:
How can you delete doubles of an element in a vector, all elements
check and then delete the extra ones that are in the list.


// You'd probably be better off working with iterators only
// (instead of containers), but here's the general idea.

template< typename C >
void remove_duplicates( C& c )
{

std::map<typename C::element_type, bool> seen;
C unique_elements;
C::const_iterator p = c.begin( );

while( p != c.end( ) )
{
if( ! seen[ *p ] )
{
unique_elements.push_back( *p );
seen[ *p ] = true;
}

++p;
}

orig.swap( unique_elements );
}

Jul 22 '05 #2

P: n/a
On 13 Dec 2003 12:57:54 -0800, sn***@hotmail.com (Tom) wrote:
How can you delete doubles of an element in a vector, all elements
check and then delete the extra ones that are in the list.


If you're going to erase, insert stuff at random places it's better to
use a list then a vector.
Jul 22 '05 #3

P: n/a
Tom wrote:
How can you delete doubles of an element in a vector, all elements
check and then delete the extra ones that are in the list.


If you are willing to sort the vector then the following works well.

#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>

int main ( ) {
// Initialise vector
int list[] = { 1,2,3,4,5,4,3,2,1,2,3,4,5 };
std::vector<int> a ( list, list + sizeof(list)/sizeof(int) );

// Print contents
std::copy ( a.begin(), a.end(),
std::ostream_iterator<int>(std::cout," "));
std::cout << '\n';

// Unique requires a sorted list
std::sort ( a.begin(), a.end() );

// Move duplicates to end of list
std::vector<int>::iterator end = std::unique ( a.begin(), a.end() );

// Erase duplicates
a.erase ( end, a.end() );

// Print new contents
std::copy ( a.begin(), a.end(),
std::ostream_iterator<int>(std::cout," "));
std::cout << '\n';
}
Jul 22 '05 #4

P: n/a
I answered another question of yours with a similar suggestion ... unless
you must use a vector for some reason, consider using an std::set. On
insert, it will return an error if the element already exists. Then you can
choose to delete the existing one and re-do the insert. (Some
implementations of std::set use operator[] to automatically replace an
existing element with the new one, but I'm not sure if that's standard for
all implementations.)

Christine

"Tom" <sn***@hotmail.com> wrote in message
news:78**************************@posting.google.c om...
How can you delete doubles of an element in a vector, all elements
check and then delete the extra ones that are in the list.

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.