3

set & lt; T & gt; vs set & lt; T, comparator & gt; (C ++ Polymor...

 2 years ago
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.
neoserver,ios ssh client

set & lt; T & gt; vs set & lt; T, comparator & gt; (C ++ Polymorphism)

advertisements

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

In another thread I found this comparator (bottom of post) for sorting JTable columns that could be composed of integers, strings, or both. I cannot figure out how to apply it to my JTable. My table was using the auto created row sorter before. I set

How to create a set std :: set with custom comparator in C ++?

How do I create a set of pairs, the elements of which (the pairs) are sorted with a custom bool function? I write set <pair<int,int>,compare> myset; and get error : Type/value mismatch at argument 2, expected a type, got "compare" I

std :: set & lt; Key, Comparator, Allocator & gt;

I've tried to use set to keep some configurations in a trie lately. I've found a doubt on the comparator, for instance : #include <iostream> #include <set> using namespace std; struct Node{ int position; int reference; Node(int r, int p){ this

std :: set selects a comparator smaller or larger at runtime

I was refactoring some code and found there are two places that can be written with the same code except the comparator of a set is less<double> in one place and greater<double> in the other. Something like: double MyClass::Function1(double va

javascript - app.set ('port', 8080) compared to app.listen (8080) in Express.js

I've been trying to use Express.js to launch a website. At first, I was using app.set('port', 8080) but the browser was unable to connect to the page. Afterwards, I changed the code to app.listen(8080) and the webpage appeared normally. This led me t

Can I give supreme importance to a set of styles compared to other style statements?

Is it possible to give a whole set of styles supreme importance? Ie, early on you might have the following default css: <link rel="stylesheet" href="/style.css" type="text/css" media="screen" /> and inside it

Java Set of results - Compare Java date with SQL getDate ()

Codes for getting current date: SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd"); Date now = new Date(); String strDate = sdfDate.format(now); return strDate; Codes to search date and return result try { String x = cDate(); ps = con

Setting up a comparator for a primitive Boolean type?

I need some classes implements Comparator, and for one I want to compare primitive boolean (not Boolean) values. IF it was a *B*oolean, I would just return boolA.compareTo(boolB); which would return 0, -1 or 1. But how can I do this with primitives?Y

How to set up a comparable interface?

I have a main class -- Simulator -- that uses two other classes -- Producer and Evaluator. Producer produces results, while the Evaluator evaluates those results. The Simulator controls the flow of execution by querying the Producer and then conveyin

Clean & ldquo; Prefixes & rdquo; In a set of tables?

I was wondering, I've got a Set of Arrays containing something of my own datatype. something looking like: traces = {[<label1>, <label2>], [<label1>], [<label1>,<label2>,<label3>]} Now, I would like to have a method tha

Polymorphic set with polymorphic comparison from Fun Set Set Set

Jane Street Core has a very handy 'a Core.Set.Poly.t which is a polymorphic set based on polymorphic Pervasives.compare. For example, this set has a map function that maps 'a -> 'b. For comparison standard OCaml set is functor-based and produces a mo

How do I return a std :: set with a private comparator

I'd like to write a (C++) method that returns an std::set of custom objects. I do not however want to expose the comparator used when inserting the objects, so I make it a private class. I create the set like this: std::set<some_class, some_class_com

Fast algorithm to select two intervals of this set

Suppose we are given a set of closed intervals where each interval is in the form of [l,r]. If we want to select two intervals from this set such that the size of their intersection times the size of their union is the maximum. Can we provide a nontr

WPF DateTimePicker does not set the date after validation

I'm trying to validate the selected date in a datetime picker control and setting it to today's date if the date selected is > Datetime.Today.The issue I'm facing is that I'm not able to set the SelectedDate property of a datetimepicker control via x

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK