CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Nov 2010
    Posts
    146

    multimap: find duplicates

    ...
    Last edited by gulHK; April 14th, 2011 at 10:32 AM.

  2. #2
    Join Date
    Mar 2008
    Location
    Turin / Italy
    Posts
    178

    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.

  3. #3
    Join Date
    Nov 2010
    Posts
    146

    Re: multimap: find duplicates

    ...
    Last edited by gulHK; April 14th, 2011 at 10:32 AM.

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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.

  5. #5
    Join Date
    Nov 2010
    Posts
    146

    Re: multimap: find duplicates

    ...
    Last edited by gulHK; April 14th, 2011 at 10:32 AM.

  6. #6
    Join Date
    Mar 2008
    Location
    Turin / Italy
    Posts
    178

    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;
    }

  7. #7
    Join Date
    Nov 2010
    Posts
    146

    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.

  8. #8
    Join Date
    Mar 2008
    Location
    Turin / Italy
    Posts
    178

    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

  9. #9
    Join Date
    Nov 2010
    Posts
    146

    Re: multimap: find duplicates

    ...
    Last edited by gulHK; April 14th, 2011 at 10:32 AM.

  10. #10
    Join Date
    Mar 2008
    Location
    Turin / Italy
    Posts
    178

    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.

  11. #11
    Join Date
    Oct 2010
    Posts
    106

    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
  •  





Click Here to Expand Forum to Full Width

Featured