•
•
•
•
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
![]() |
•
•
Join Date: Dec 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
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?
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.
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.
CPP Syntax (Toggle Plain Text)
/* less_comparator - user-defined comparison */ template <typename T> struct less_comparator { bool operator()(T, T) const; }; template <typename T> bool less_comparator<T>::operator() (T lhs, T rhs) const { return lhs < rhs; } template<typename T> class structure { public: structure() {} template<class Comparator> structure( Comparator comp ) { } }; int main() { structure<int> s( less_comparator<int> ); }
Last edited by Bench : Dec 16th, 2007 at 9:01 am.
¿umop apisdn upside down? Additional:
It seems I was half-right, although, the STL uses the
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.
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? ![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
access advice breach broadband business classification code combo crime daniweb data data protection data transfer database drive dropdownlist encryption europe forensic forensics fun government hard hardware help hitachi hp industrial espionage information internet it linux module net news payment services privacy protection reuse security storage terabyte tutorials and more web wikipedia you tried to assign the null value to a variable that is not a variant data type
- Previous Thread: Count/Find in const char *
- Next Thread: Help regarding MSI Serial Key Validation


Linear Mode