473,473 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Finding dupilcate entry in a STL

Hi,

I have a function which check if there is duplicate entries in a STL
list:

bool hasDuplicateY() {

sort(_aList.begin(), _aList.end(), sort_y_comparator<A*>());
AList::iterator iter;

iter = adjacent_find ( _aList.begin(), _aList.end(), SameY());

if (iter == _aList.end())

return false;
else
return true;
}

bool SameY::operator()( A* bd1, A* bd2)
{
return bd1->y == bd2->y;
}
template<class T> struct sort_y_comparator : public
binary_function<T,T,bool>
{
sort_y_comparator() {}

bool operator()(const T r1, const T r2) const
{
return (r1->y < r2->y);
}
};
But the above function always return true even if there is no
duplication in the list.
Can you please tell me what have I done wrong?

Thank you.

Jan 30 '06 #1
5 1563
ke*********@gmail.com wrote:
I have a function which check if there is duplicate entries in a STL
list:

bool hasDuplicateY() {

sort(_aList.begin(), _aList.end(), sort_y_comparator<A*>());
You can't use std::sort on a std::list, because a std::list doesn't
have random access iterators. I'm surprised this even compiled. Use
std::list member function list::sort() instead.
AList::iterator iter;

iter = adjacent_find ( _aList.begin(), _aList.end(), SameY());

if (iter == _aList.end())

return false;
else
return true;
Why not just:

AList::iterator iter = adjacent_find(_aList.begin(), _aList.end(),
SameY());
return iter!=_aList.end();

or even just:

return adjacent_find(_aList.begin(),
_aList.end(),
SameY()) != _aList.end();
}

bool SameY::operator()( A* bd1, A* bd2)
{
return bd1->y == bd2->y;
}
template<class T> struct sort_y_comparator : public
binary_function<T,T,bool>
{
sort_y_comparator() {}
Many would recommend that you omit that constructor if the default will
do just fine.

bool operator()(const T r1, const T r2) const
{
return (r1->y < r2->y);
}
};
But the above function always return true even if there is no
duplication in the list.
Can you please tell me what have I done wrong?


First, fix the problem with std::sort. Then, if you're still having
the problem, re-post, this time including code we can actually compile,
rather than just a snippet.

Good luck.

Best regards,

Tom

Jan 30 '06 #2
Thomas Tutone wrote:
Many would recommend that you omit that constructor if the default will
do just fine.


I meant to say that many would recommend that you omit that constructor
if the compiler-generated constructor will do just fine.

Best regards,

Tom

Jan 30 '06 #3
ke*********@gmail.com wrote:
Hi,

I have a function which check if there is duplicate entries in a STL
list:

bool hasDuplicateY() {

sort(_aList.begin(), _aList.end(), sort_y_comparator<A*>());
AList::iterator iter;


If you are using std::list they model the Reversible Containers who's
iterators are only bidirectional iterators. The sort you have used in
you program is a general algorithm applied to containers that support
Random Iterators. In order to sort the list of type std::list you can
use the sort provided by std::list using bidirectional iterators.

Change the following
sort(_aList.begin(), _aList.end(), sort_y_comparator<A*>());
TO
_aList.sort(sort_y_comparator<A*>()); ==> Will sort your list.

-- Nitin Motgi

Jan 30 '06 #4
ke*********@gmail.com wrote:
Hi,

I have a function which check if there is duplicate entries in a STL
list:

bool hasDuplicateY() {

sort(_aList.begin(), _aList.end(), sort_y_comparator<A*>());
AList::iterator iter;


If you are using std::list they model the Reversible Containers who's
iterators are only bidirectional iterators. The sort you have used in
you program is a general algorithm applied to containers that support
Random Iterators. In order to sort the list of type std::list you can
use the sort provided by std::list using bidirectional iterators.

Change the following
sort(_aList.begin(), _aList.end(), sort_y_comparator<A*>());
TO
_aList.sort(sort_y_comparator<A*>()); ==> Will sort your list.

-- Nitin Motgi

Jan 30 '06 #5
In article <11*********************@g14g2000cwa.googlegroups. com>,
ke*********@gmail.com wrote:
Hi,

I have a function which check if there is duplicate entries in a STL
list:

bool hasDuplicateY() {

sort(_aList.begin(), _aList.end(), sort_y_comparator<A*>());
AList::iterator iter;

iter = adjacent_find ( _aList.begin(), _aList.end(), SameY());

if (iter == _aList.end())

return false;
else
return true;
}
As others have already said, the 'sort' above should be
_aList.sort(...), and this can be written much more compactly...

_aList.sort(sort_y_comparator<A*>());
return adjacent_find(_aList.begin(), _aList.end(), SameY()) !=
aList.end();

bool SameY::operator()( A* bd1, A* bd2)
{
return bd1->y == bd2->y;
}
template<class T> struct sort_y_comparator : public
binary_function<T,T,bool>
{
sort_y_comparator() {}

bool operator()(const T r1, const T r2) const
{
return (r1->y < r2->y);
}
};


I wouldn't make the above a template, the only types it can take are
ones that can handle "->y" which is pretty limited...

Did you notice how your sort_y_comparator and your 'SameY' look alot
alike? The only difference between them is the actual compare ('==' in
one and '<' in the other.) I would think you could factor out that
duplication...

struct A {
int y;
};

// better would be to make A a class and make this a member-function
int& getY( A* a ) {
return a->y;
}

bool hasDuplicateY( list<A*>& aList )
{
aList.sort(f_gx_gy(less<int>(), ptr_fun(&getY)));
return adjacent_find(aList.begin(), aList.end(),
f_gx_gy(equal_to<int>(), ptr_fun(&getY))) != aList.end();
}

To make the above work, you need a composer like this:

template <typename Op1, typename Op2, typename Op3>
class f_gx_hy_t: public std::binary_function<typename
Op2::argument_type, typename Op3::argument_type, typename
Op1::result_type>
{
Op1 fn1;
Op2 fn2;
Op3 fn3;
public:
f_gx_hy_t(const Op1& f, const Op2& g, const Op3& h):
fn1(f), fn2(g), fn3(h) { }

typename Op1::result_type operator()(const typename
Op2::argument_type& x, const typename Op3::argument_type& y) const {
return fn1(fn2(x), fn3(y));
}
};

template <typename Op1, typename Op2>
inline f_gx_hy_t<Op1, Op2, Op2>
f_gx_gy(const Op1& f, const Op2& g) {
return f_gx_hy_t<Op1, Op2, Op2>(f, g, g);
}
Presumably, this can be done even simpler using the boost::lambda
library.
<http://www.boost.org/doc/html/lambda.html>

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Jan 31 '06 #6

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

Similar topics

3
by: KL | last post by:
Well, I am back. This time our assignment has us filling a vector and then timing how long it takes to find a spot in the vector to insert a new item, and the time required to insert the item...
2
by: Antitax | last post by:
I have a database with more than 800 adress records Some of the are similar because some letters in the street adress for example are not identical, altough they point to the same adress. Does...
3
by: D Denholm | last post by:
I am a Access newbie... Hopefully somebody can help me figure this out. I have a database that looks like: Asset Economic Minimum ----- ---------------- 10555 ...
0
by: Jay | last post by:
Good morning, I admit, I'm stuck any help in this would be greatly appreciated. I have a searcher.filter that is currently pulling all objects of type 'person' from my Active Directory, but I...
1
by: | last post by:
Hello, I'm keping myself busy here by investigating the world of LDAP and Active Directory. I have an application which allows users from the system to be selected from a list box and as a...
9
by: Laurent Bugnion | last post by:
Hi, I am wondering what is the best way to find out which ASP.NET sessions are still active. Here is the reason: I have a custom control which can upload files. It saves the files in a folder...
19
by: ramu | last post by:
Hi, I have, suppose 1000 numbers, in a file. I have to find out 5 largest numbers among them without sorting. Can you please give me an efficient idea to do this? My idea is to put those numbers...
3
by: UJ | last post by:
How can I find out when the machine last rebooted and where it was a system crash or an regular reboot. This would be on XP. TIA - Jeff.
1
by: colinw | last post by:
I need to query a table of data that has multiple datetime entries in it relating to individual customer records. So one customer could have 1 entry, or it could have 10 entries. I need to be able to...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
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...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.