Multiset with custom < operator - problem with iterators

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • orzeech
    New Member
    • Nov 2008
    • 5

    #1

    Multiset with custom < operator - problem with iterators

    Hi everyone!

    I have a following problem:

    Code:
    #include<iostream>
    #include<set>
    
    using namespace std;
    
    struct punkt
    {
           int x;
           int y;
        
    };
    
    struct porownaj_x {
      bool operator() (const punkt& lhs, const punkt& rhs) const
      {return lhs.x<rhs.x;}
    };
    
    struct porownaj_y {
      bool operator() (const punkt& lhs, const punkt& rhs) const
      {return lhs.y<rhs.y;}
    };
    
    int main()
    {
        
    
        int z;
        cin >> z;
    
        int a,b;
        punkt zabytki[z];
        for (int i=0; i<z; i++)
        {
        cin >> a >> b;
        //tab[a][b]=1;
        zabytki[i].x=a; 
        zabytki[i].y=b;
        }
        
            
        multiset<punkt,porownaj_x> sx;
        multiset<punkt,porownaj_y> sy;
    
        for (int i=0; i<z; i++)
        { sx.insert(zabytki[i]);
          sy.insert(zabytki[i]);}
    
       multiset<punkt, porownaj_x>::iterator itt;
    
    int ub = 10;
    for (itt=sx.upper_bound(ub); itt!=sx.end(); itt++)
        cout << " " << (*itt).x << " " << (*itt).y;
    cin.get();
    cin.get();
    return 0;
    }

    Compiler returns an error: 51 no matching function for call to `std::multiset< punkt, porownaj_x, std::allocator< punkt> >::upper_bound( int&)'

    Multisets are working perfectly fine but upper_bound and lower_bound iterators aren't:( What is wrong in the code?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Your multiset has punkt as the key.

    Code:
    multiset<punkt,porownaj_x> sx;
    That means your iterator needs a punkt as a key. However, you are using ub which is an int. A punkt and an int are different types.Hence the compiler error.

    Comment

    • boxfish
      Recognized Expert Contributor
      • Mar 2008
      • 469

      #3
      I think it's because you're passing an int to the upper_bound function. You have to give it an object of type punkt, its key type. I don't actually know what a multiset is; I just looked it up here.

      Comment

      • orzeech
        New Member
        • Nov 2008
        • 5

        #4
        Originally posted by weaknessforcats
        Your multiset has punkt as the key.

        Code:
        multiset<punkt,porownaj_x> sx;
        That means your iterator needs a punkt as a key. However, you are using ub which is an int. A punkt and an int are different types.Hence the compiler error.

        Thank you very very much for the quick reply!!! :)

        Comment

        Working...