Thread: std::set<boost::weak_ptr> and find()

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    std::set<boost::weak_ptr> and find()

    I'm curious:

    weak_ptr doesn't provide the user with operator== and yet I'm able to successfully use the find member function of std::set.

    I've done some debugging to try and see how this is happening, and apparently weak_ptr is not even being converted to a shared_ptr in order to do the equality check. I went as far down as stl_tree.h where the actual equality check is being done. At that point a pointer to weak_ptr has been created.

    What exactly determines that an std::set find() member function works with weak pointers, but not a std::vector one?
    Last edited by Mario F.; 07-31-2006 at 05:07 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > What exactly determines that an std::set find() member function works with weak pointers, bit not a std::vector one?
    sets, as well as other associative STL containers, store their keys in sorted order to allow fast traversal, so if you searched for something in the set you can actually use one of the other comparison operators to find something. This is why weak pointers don't work with vector well, because vector is strictly random access and not always sorted.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What exactly determines that an std::set find() member function works with weak pointers, but not a std::vector one?
    std::vector doesn't have a find member function, to start with. You would have to use the find function from <algorithm>. By default, std::set uses the equivalent of operator< for it's relational tests, an operator which boost::weak_ptr implements.
    My best code is written with the delete key.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I actually wanted to mean the find algorithm for std::vector. My bad.

    Thanks both. Didn't think the find() member function wouldn't use ==, despite being a vector.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    With the vector you can use the lagorithms binary_search, lower_bound, upper_bound and/or equal_range to utilize operator< instead of operator==.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. could not find -lwsock32.lib
    By thomas_joyee in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2008, 12:28 PM
  3. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM