User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 422,675 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,694 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 797 | Replies: 3 | Solved
Reply
Join Date: Dec 2007
Posts: 2
Reputation: Queue is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Queue Queue is offline Offline
Newbie Poster

Help Compare in templatized data structure

  #1  
Dec 16th, 2007
After writing out the main functionality for my templatized data structure, I am stumbling with the syntax to allow for Compare (so the user can provide a definition for less). If I am not mistaken, I need to add at least another constructor. I tried reading the STL implementations for priority_queue and the such, but the syntax looked rather cryptic. Can someone please either point me in the direction of decent documentation (since I cannot seem to find any) or provide me with instructions?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Location: UK
Posts: 468
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Rep Power: 5
Solved Threads: 42
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Compare in templatized data structure

  #2  
Dec 16th, 2007
I could be mistaken, though I believe that the STL sorted containers expect a type with a valid operator<. If the type doesn't provide one, then overloaded constructor(s) will take an additional parameter in the form of a compare function, or a function object which overloads operator() instead.

Since you don't know the precise type of the comparator in advance, you need to provide a templated constructor where any kind of comparator can be used to compare objects within the data structure


Here's a brief example of a constructor accepting a functor, whose type is determined at compile time.
  1. /* less_comparator - user-defined comparison */
  2. template <typename T>
  3. struct less_comparator
  4. {
  5. bool operator()(T, T) const;
  6. };
  7.  
  8. template <typename T>
  9. bool less_comparator<T>::operator() (T lhs, T rhs) const
  10. {
  11. return lhs < rhs;
  12. }
  13.  
  14. template<typename T>
  15. class structure
  16. {
  17. public:
  18. structure() {}
  19. template<class Comparator>
  20. structure( Comparator comp )
  21. {
  22. }
  23. };
  24.  
  25. int main()
  26. {
  27. structure<int> s( less_comparator<int> );
  28. }
Last edited by Bench : Dec 16th, 2007 at 9:01 am.
¿umop apisdn upside down?
Reply With Quote  
Join Date: Feb 2006
Location: UK
Posts: 468
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Rep Power: 5
Solved Threads: 42
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Compare in templatized data structure

  #3  
Dec 16th, 2007
Additional:

It seems I was half-right, although, the STL uses the std::less<T> functor class as its default predicate for sorted containers, as found in the <functional> header. (std::less in turn assumes that operator< is available)

The STL also takes the extra step of enforcing the predicate to match a certain type (Which the example I gave doesn't do), using an additional template parameter, which defaults to std::less<T>. This is presumably a safety net to stop predicates for different types being used accidentally.
¿umop apisdn upside down?
Reply With Quote  
Join Date: Dec 2007
Posts: 2
Reputation: Queue is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Queue Queue is offline Offline
Newbie Poster

Re: Compare in templatized data structure

  #4  
Dec 17th, 2007
I ended up using the std::less<T> functor, which worked perfectly for my implementation. Thank you very much, Bench!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:36 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC