set & lt; T & gt; vs set & lt; T, comparator & gt; (C ++ Polymor...
source link: https://www.codesd.com/item/set-t-vs-set-t-comparator-c-polymorphism.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
set & lt; T & gt; vs set & lt; T, comparator & gt; (C ++ Polymorphism)
Why does this code
struct ThingComparator
{
...
}
static void Blah (set <CString> &things)
{
...
}
...
set<CString, ThingComparator>things;
Blah (things);
fail to compile with the following error (Visual Studio 2010):
error C2664: 'Blah' : cannot convert parameter 1 from 'std::set<_Kty,_Pr>' to 'std::set<_Kty> &'
My C++ knowledge is obviously limited, but I expected to hear a trumpet announcing the polymorphism knight on his trusty steed, but instead all I can hear is a horse fart and a sad trombone :-(
std::set
is declared as follows:
template<
class Key,
class Compare = std::less<Key>,
class Allocator = std::allocator<Key>
> class set;
Hence std::set<CString>
really means std::set<CString, std::less<CString>, std::allocator<CString>>
, and std::less<CString>
isn't ThingComparator
. Write the following instead:
struct ThingComparator {
...
};
template<typename Comparator>
static void Blah(std::set<CString, Comparator>& things) {
...
}
...
std::set<CString, ThingComparator> things;
Blah(things);
Related Articles
Troubleshooting to Set a Column Comparator on a JTable
How to create a set std :: set with custom comparator in C ++?
std :: set & lt; Key, Comparator, Allocator & gt;
std :: set selects a comparator smaller or larger at runtime
javascript - app.set ('port', 8080) compared to app.listen (8080) in Express.js
Can I give supreme importance to a set of styles compared to other style statements?
Java Set of results - Compare Java date with SQL getDate ()
Setting up a comparator for a primitive Boolean type?
How to set up a comparable interface?
Clean & ldquo; Prefixes & rdquo; In a set of tables?
Polymorphic set with polymorphic comparison from Fun Set Set Set
How do I return a std :: set with a private comparator
Fast algorithm to select two intervals of this set
WPF DateTimePicker does not set the date after validation
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK