473,626 Members | 3,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to remove duplicate entries in a vector of Pair?

I have a vector of Pair of int:

typedef pair<int, int> MyPair;
typedef vector <MyPair > MyVector

I would like to remove entries if their first are equal, or if their
value is swap ( first of first pair equals to seconds of second pair):

bool equals(MyPair src, MyPair dest ) {
if (( src.first == dest.first) && ( src.second == dest.second))
return true;

if (( src.first == dest.second) && ( src.second == dest.first))
return true;

return false;
}

I think I want to use the 'unique' algorithm and use the above 'equals'
function as the binary predicate.
But before I use the 'unique' algorithm, I need to sort my list. But
for my purpose, I am not sure how to sort my list. base on the 'first'
value? or base on the second value?

Thank you for any pointers.

Mar 25 '06 #1
1 5245
Al************@ gmail.com wrote:
I have a vector of Pair of int:

typedef pair<int, int> MyPair;
typedef vector <MyPair > MyVector

I would like to remove entries if their first are equal, or if their
value is swap ( first of first pair equals to seconds of second pair):

What you say above is not equivalent to what you write below, but I'll
assume that what you mean is what you've implemented below.
bool equals(MyPair src, MyPair dest ) {
if (( src.first == dest.first) && ( src.second == dest.second))
return true;

if (( src.first == dest.second) && ( src.second == dest.first))
return true;

return false;
}

I think I want to use the 'unique' algorithm and use the above 'equals'
function as the binary predicate.
But before I use the 'unique' algorithm, I need to sort my list. But
for my purpose, I am not sure how to sort my list. base on the 'first'
value? or base on the second value?


Sort by the smaller of the two pair elements, resolving ties by the
larger of the two pair elements.

Mark
Mar 25 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2011
by: John Black | last post by:
Hi, I am not familiar with for_each very well, suppoase I have a vector<pair<unsigned int, unsigned int> > vec1 and the contents are {<0x00000000, 0x000000FF>, <0x10000000, 0x2FFFFFFF>} what I want is to create another vector, vector<int> vec2, which store all the first 8 bit heads of the integer in vec1, for the above example, 0x00000000 & 0xFF000000 ==> 0x00000000,
11
2734
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
9
6561
by: John | last post by:
How do I copy all the elements of a map to a vector efficiently? The vector is empty initially and needs to be populated with the map elements in sorted order. Thanks, --j
5
12048
by: ma740988 | last post by:
For starters, Happy New Year to all!! I created a vector of pairs where pair first is a primitive and pair second is a vector of ints. So now: # include <iostream> # include <vector>
14
21962
by: BarrySDCA | last post by:
I have a database being populated by hits to a program on a server. The problem is each client connection may require a few hits in a 1-2 second time frame. This is resulting in multiple database entries - all exactly the same, except the event_id field, which is auto-numbered. I need a way to query the record w/out duplicates. That is, any records exactly the same except event_id should only return one record. Is this possible??
7
9969
by: Allerdyce.John | last post by:
How can I emove an item in STL iterator without use the STL algorithm? I know we can use stl erase, find_if to remove items from a STL vector , but how can I do the same without using STL algorithm? vector<int> srcVector; vector<int> destVector; for (vector<int>::iterator itr = srcVector; itr != srcVector; itr++) { int i = (*itr);
5
3987
by: Manish | last post by:
The topic is related to MySQL database. Suppose a table "address" contains the following records ------------------------------------------------------- | name | address | phone | ------------------------------------------------------- | mr x | 8th lane | 124364 | | mr x | 6th lane | 435783 | | mrs x | 6th lane | 435783 |
13
2065
by: arnuld | last post by:
this is the code: ------------------------------------------------------------------------- #include <iostream> #include <string> #include <vector> struct Pair { std::string name;
4
2441
by: ejack | last post by:
int total = static_cast<int>(vComp.size()) - 1; int tTotalUnits = 0; float tTotalCostJob = 0; int i = 0; int j = 0; int counter = 0; bool foundFlag; vector<string> vItems; vector<string> vHolding;
0
8202
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8641
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2628
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.