Results 1 to 3 of 3

Thread: map(stl) in c++

  1. #1
    Join Date
    Jul 2008
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default map(stl) in c++

    hi

    i have declared a structure having members as string and int.
    now how can i make a function for comparision so that i used it in map.
    map<key,value,function>

    in function field i need user define function which takes one argument as string and other as int.


    please help me out.

    thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: map(stl) in c++

    Could you post the exact definition of your map? The key comparison function takes two keys as parameters, not values.

  3. #3
    Join Date
    Apr 2008
    Location
    Buenos Aires, Argentina
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: map(stl) in c++

    You can:

    1) If your key is a structure you have created you can create the comparison function like this:

    Qt Code:
    1. bool operator<(const YourType& k0, const YourType& k1)
    2. {
    3. //do stuff...
    4. //return true if k0 is LESS than k1, if k0 is equal or more than k1 return false
    5. }
    To copy to clipboard, switch view to plain text mode 

    2) Pass a compare function when you create the map. Read this it will explained it very well.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.