-
April 12th, 2011, 05:21 AM
#1
multimap: find duplicates
Last edited by gulHK; April 14th, 2011 at 10:32 AM.
-
April 12th, 2011, 05:50 AM
#2
Re: multimap: find duplicates
use equal_range
http://www.cplusplus.com/reference/s...p/equal_range/
It returns you a pair of two iterators consisting in lower_bound and upper_bound of the elements found.
Or use multimap::count to find how many elements have the same key.
http://www.cplusplus.com/reference/stl/multimap/count/
Last edited by SkyNetTo; April 12th, 2011 at 05:56 AM.
-
April 12th, 2011, 07:10 AM
#3
Re: multimap: find duplicates
Last edited by gulHK; April 14th, 2011 at 10:32 AM.
-
April 12th, 2011, 07:47 AM
#4
Re: multimap: find duplicates
Equal range and count work, but might not be very efficient: They require you know the key, and then parse your container every time for every key.
I'd recommend just iterating over every element, and do something if the current iterator is the same as the previous:
Code:
#include <iostream>
const int a[] = {1,2,2,2,3,4,4,5,5,6,7,8};
const size_t size = sizeof(a)/sizeof(a[0]);
typedef const int* iterator;
int main()
{
iterator curr = a;
iterator last = a + size;
std::cout << "Printing all dupes, including the first." << std::endl;
for( ; curr != last ; )
{
iterator next = curr;
if( ++next != last && *next == *curr )
{
std::cout << *curr << " " << *next;
while( ++next != last && *next == *curr )
{std::cout << *next << " ";}
}
curr = next;
}
}
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
April 12th, 2011, 08:37 AM
#5
Re: multimap: find duplicates
Last edited by gulHK; April 14th, 2011 at 10:32 AM.
-
April 12th, 2011, 09:34 AM
#6
Re: multimap: find duplicates
how about this?
Code:
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string key;
multimap<string,string> m;
m.insert( make_pair("12011-01-24T19:28:52-05:00IFEU20111100FUTBIFEU20111100FUTB3425P","k1") );
m.insert( make_pair("12011-01-24T19:28:52-05:00IFEU20111100FUTBIFEU20111100FUTB3425P","k2") );
m.insert( make_pair("12011-01-24T19:28:52-05:00IFEU20111200FUTB","k5") );
m.insert( make_pair("12011-01-24T19:28:52-05:00IFEU20120200FUTB","k5") );
m.insert( make_pair("12011-01-24T19:28:53-05:00IFEU20111100FUTB","k3") );
m.insert( make_pair("12011-01-24T19:28:53-05:00IFEU20120200FUTB","k3") );
key = m.begin()->first;
multimap<string,string>::const_iterator itL;
multimap<string,string>::const_iterator itU;
do
{
itL = m.lower_bound(key);
itU = m.upper_bound(key);
while (itL != itU )
{
cout << (itL->first) << endl;
++itL;
}
if(itU != m.end())
key = itU->first;
}while(itL != m.end());
return 0;
}
-
April 12th, 2011, 09:39 AM
#7
Re: multimap: find duplicates
Thanks SkyNetTo. But the result is not what I want
I want it to print just these keys, because only these values are duplicates
Code:
12011-01-24T19:28:52-05:00IFEU20111100FUTBIFEU20111100FUTB3425P
12011-01-24T19:28:52-05:00IFEU20111100FUTBIFEU20111100FUTB3425P
Last edited by gulHK; April 14th, 2011 at 10:16 AM.
-
April 12th, 2011, 09:46 AM
#8
Re: multimap: find duplicates
This is my solution. It's not so elegant but it works. Maybe someone else will post a more clever solution.
Code:
#include <map>
#include <string>
#include <iostream>
#include <functional>
using namespace std;
int main()
{
string key;
multimap<string,string> m;
m.insert( make_pair("12011-01-24T19:28:52-05:00IFEU20111100FUTBIFEU20111100FUTB3425P","k1") );
m.insert( make_pair("12011-01-24T19:28:52-05:00IFEU20111100FUTBIFEU20111100FUTB3425P","k2") );
m.insert( make_pair("12011-01-24T19:28:52-05:00IFEU20111200FUTB","k5") );
m.insert( make_pair("12011-01-24T19:28:52-05:00IFEU20120200FUTB","k5") );
m.insert( make_pair("12011-01-24T19:28:53-05:00IFEU20111100FUTB","k3") );
m.insert( make_pair("12011-01-24T19:28:53-05:00IFEU20120200FUTB","k3") );
key = m.begin()->first;
multimap<string,string>::const_iterator itL;
multimap<string,string>::const_iterator itU;
do
{
itL = m.lower_bound(key);
itU = m.upper_bound(key);
if(itL!= m.end()) //change from itU to itL
{
multimap<string,string>::difference_type diff = std::distance(itL,itU);
if(diff > 1)
{
while (itL != itU )
{
cout << (itL->first) << endl;
++itL;
}
}
}
if(itU != m.end())
key = itU->first;
}while(itU != m.end());
return 0;
}
Last edited by SkyNetTo; April 12th, 2011 at 09:55 AM.
Reason: bug found
-
April 12th, 2011, 10:35 AM
#9
Re: multimap: find duplicates
Last edited by gulHK; April 14th, 2011 at 10:32 AM.
-
April 12th, 2011, 10:45 AM
#10
Re: multimap: find duplicates
Code:
else if(the keys of MAP-1 are all the SAME)
you can find this by comparin the map.begin()->first count with map size.
Code:
if(m1.count(m1.begin()->first) == m1.size())
// all keys are the same.
else
{
//keys are different but may have duplicates, so you don't want to print these duplicates but only the keys that appeared once.
for(it = m1.begin(); it != m1.end(); ++it)
{
if(m1.count(it->first) == 1)
//print
}
// this is a quick approach but has a complexity O(nlogn)
}
Last edited by SkyNetTo; April 12th, 2011 at 12:15 PM.
-
April 13th, 2011, 03:15 AM
#11
Re: multimap: find duplicates
Thank you SkyNetTo
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|