STL find & compare

Discussion in 'C++' started by Tom, Dec 2, 2003.

  1. Tom

    Tom Guest

    Hi,
    I need some help with stl maps.

    I am using a std::map that is sorted in a very special way.
    Therefore I am using my own sort function. That works fine.
    But this is only half the problem. Additionally "map.find()" should
    use a different comparison function.

    My map is like:
    map < myString, Whatever>
    As far as I know "find" uses operators from the key (myString).
    Therefore I wrote my own operator== ,operator< and operator>
    Nothing worked. Does anybody know which comparison
    function uses "find" or know any other solution while
    using std functionality ?

    thanks a lot T.
    Tom, Dec 2, 2003
    #1
    1. Advertising

  2. Tom

    c wood Guest

    "Tom" <> wrote in message
    news:bqifl3$hn0$...
    > Hi,
    > I need some help with stl maps.
    >
    > I am using a std::map that is sorted in a very special way.
    > Therefore I am using my own sort function. That works fine.
    > But this is only half the problem. Additionally "map.find()" should
    > use a different comparison function.
    >
    > My map is like:
    > map < myString, Whatever>
    > As far as I know "find" uses operators from the key (myString).
    > Therefore I wrote my own operator== ,operator< and operator>
    > Nothing worked. Does anybody know which comparison
    > function uses "find" or know any other solution while
    > using std functionality ?


    Maintain another index, sorted by the "real" value you want to find by.

    By index I mean map. I usually put my elements in a set, ordered by pri
    key, and add a map<2nd key, item *> index. Otherwise you'll end up
    iterating through them all looking for a certain value.

    Of course this all depends on the details of exactly what you are doing
    too. If you elaborate a little more into the details, perhaps we can help
    you better.
    c wood, Dec 2, 2003
    #2
    1. Advertising

  3. "Tom" <> wrote in message
    news:bqifl3$hn0$...
    | I am using a std::map that is sorted in a very special way.
    | Therefore I am using my own sort function. That works fine.
    | But this is only half the problem. Additionally "map.find()" should
    | use a different comparison function.
    |
    | My map is like:
    | map < myString, Whatever>
    | As far as I know "find" uses operators from the key (myString).
    | Therefore I wrote my own operator== ,operator< and operator>
    | Nothing worked. Does anybody know which comparison
    | function uses "find" or know any other solution while
    | using std functionality ?

    map.find() uses the comparison function that is used for sorting
    ( an item that comes neither before nor after is considered equal).
    This allows the search to be performed in O(logN) time using
    the map's tree structure.

    If you want to use another equality criterion, you can use
    the std::find function:
    std::find( map.begin(), map.end(), MyPredicate(someKey) );
    Note that this search will take linear O(N) time.
    The predicate object is very much like a sort function:
    its operator() shall take a MyMap::value_type& as a reference,
    and return true if the item matches your search criterion.

    hth -Ivan
    --
    http://ivan.vecerina.com
    Ivan Vecerina, Dec 2, 2003
    #3
  4. Tom wrote:
    > Hi,
    > I need some help with stl maps.
    >
    > I am using a std::map that is sorted in a very special way.
    > Therefore I am using my own sort function. That works fine.
    > But this is only half the problem. Additionally "map.find()" should
    > use a different comparison function.
    >
    > My map is like:
    > map < myString, Whatever>
    > As far as I know "find" uses operators from the key (myString).
    > Therefore I wrote my own operator== ,operator< and operator>
    > Nothing worked. Does anybody know which comparison
    > function uses "find" or know any other solution while
    > using std functionality ?


    There is an optional compare template parameter to std::map - see.

    http://www.sgi.com/tech/stl/Map.html
    Gianni Mariani, Dec 2, 2003
    #4
  5. Tom

    Tom Guest

    I am not quite sure if nesting maps will really helps in my case.
    I am using my own sort function, which works fine.

    What I need is a find function, that returns equity if a character in the
    key
    is a dot "." (Nr. 46 in ASCII table). And it should return the first entry
    that fits.

    A simplified example would be:

    Keys in Map are (in the correct order)
    "PEBKACyesitisme382"
    "PEBKAC....382"

    if my search-string is "PEBCAKyesitisme382" & it should find
    "PEBKACyesitisme382"

    if the order is v.v.
    "PEBKAC....382"
    "PEBKACyesitisme382"

    it is should find "PEBKAC....382" for "PEBCAKyesitisme382"

    it would be great if any of You knows a generic solution using stl
    implements.

    Thanks a lot
    Thomas

    "c wood" <> wrote in message
    news:XY3zb.44723$...
    >
    > "Tom" <> wrote in message
    > news:bqifl3$hn0$...
    > > Hi,
    > > I need some help with stl maps.
    > >
    > > I am using a std::map that is sorted in a very special way.
    > > Therefore I am using my own sort function. That works fine.
    > > But this is only half the problem. Additionally "map.find()" should
    > > use a different comparison function.
    > >
    > > My map is like:
    > > map < myString, Whatever>
    > > As far as I know "find" uses operators from the key (myString).
    > > Therefore I wrote my own operator== ,operator< and operator>
    > > Nothing worked. Does anybody know which comparison
    > > function uses "find" or know any other solution while
    > > using std functionality ?

    >
    > Maintain another index, sorted by the "real" value you want to find

    by.
    >
    > By index I mean map. I usually put my elements in a set, ordered by

    pri
    > key, and add a map<2nd key, item *> index. Otherwise you'll end up
    > iterating through them all looking for a certain value.
    >
    > Of course this all depends on the details of exactly what you are

    doing
    > too. If you elaborate a little more into the details, perhaps we can

    help
    > you better.
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    Tom, Dec 3, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gianni Mariani
    Replies:
    1
    Views:
    857
    Pete Becker
    Aug 16, 2003
  2. Kevin Goodsell
    Replies:
    0
    Views:
    432
    Kevin Goodsell
    Aug 16, 2003
  3. Thomas Matthews
    Replies:
    1
    Views:
    887
    John Villalovos
    Aug 18, 2003
  4. John Villalovos
    Replies:
    0
    Views:
    419
    John Villalovos
    Aug 18, 2003
  5. Replies:
    5
    Views:
    501
    Markus Schoder
    Apr 16, 2006
Loading...

Share This Page