473,387 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Need help with STL (?)

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
4 1053


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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sofia | last post by:
My name is Sofia and I have for many years been running a personals site, together with my partner, on a non-profit basis. The site is currently not running due to us emigrating, but during its...
7
by: Mike Kamermans | last post by:
I hope someone can help me, because what I'm going through at the moment trying to edit XML documents is enough to make me want to never edit XML again. I'm looking for an XML editor that has a...
15
by: drdoubt | last post by:
using namespace std In my C++ program, even after applying , I need to use the std namespace with the scope resolution operator, like, std::cout, std::vector. This I found a little bit...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
3
by: Bob.Henkel | last post by:
I write this to tell you why we won't use postgresql even though we wish we could at a large company. Don't get me wrong I love postgresql in many ways and for many reasons , but fact is fact. If...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
4
by: Phil | last post by:
k, here is my issue.. I have BLOB data in SQL that needs to be grabbed and made into a TIF file and placed on the client (could be in temp internet dir). The reason we need it in TIF format is...
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
2
by: Michael R. Pierotti | last post by:
Dim reg As New Regex("^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$") Dim m As Match = reg.Match(txtIPAddress.Text) If m.Success Then 'No need to do anything here Else MessageBox.Show("You need to enter a...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.