How to pass a comparator as a parameter for a function?How to implement classic sorting algorithms in modern C++?How do you set, clear, and toggle a single bit?How do I iterate over the words of a string?Function pointers in C - address operator “unnecessary”How can I profile C++ code running on Linux?could not deduce template argument for T[]Why do we need virtual functions in C++?C++ Pass by const reference syntaxAre the days of passing const std::string & as a parameter over?Cannot call member function without object. C++c++ override += with const reference

Does an object always see its latest internal state irrespective of thread?

How is it possible to have an ability score that is less than 3?

Is it possible to do 50 km distance without any previous training?

Today is the Center

What would happen to a modern skyscraper if it rains micro blackholes?

Do infinite dimensional systems make sense?

Alternative to sending password over mail?

strTok function (thread safe, supports empty tokens, doesn't change string)

How do I deal with an unproductive colleague in a small company?

How do I draw and define two right triangles next to each other?

Is it unprofessional to ask if a job posting on GlassDoor is real?

Can you really stack all of this on an Opportunity Attack?

What does the "remote control" for a QF-4 look like?

Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?

How can bays and straits be determined in a procedurally generated map?

What does it mean to describe someone as a butt steak?

I'm flying to France today and my passport expires in less than 2 months

Why can't we play rap on piano?

What does "Puller Prush Person" mean?

Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?

Codimension of non-flat locus

Paid for article while in US on F-1 visa?

How to determine what difficulty is right for the game?

Malcev's paper "On a class of homogeneous spaces" in English



How to pass a comparator as a parameter for a function?


How to implement classic sorting algorithms in modern C++?How do you set, clear, and toggle a single bit?How do I iterate over the words of a string?Function pointers in C - address operator “unnecessary”How can I profile C++ code running on Linux?could not deduce template argument for T[]Why do we need virtual functions in C++?C++ Pass by const reference syntaxAre the days of passing const std::string & as a parameter over?Cannot call member function without object. C++c++ override += with const reference






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have a function in a sorting header for mergeSort



This is my code:



template



void mergeSort(vector<Comparable> & a, vector<Comparable> & tmpArray, int left int right, Comparator cmp)

if (cmp(left,right))

int center = (left + right) / 2;
mergeSort(a, tmpArray, left, center);
mergeSort(a, tmpArray, center + 1, right);
merge(a, tmpArray, left, center + 1, right);




I want to use this comparator and pass in the parameters in my mainDriver.cpp



class CompareXCoordinate 
public:
bool operator()(const Point & p1, const Point & p2) const

return (p1.getX() < p2.getX());

;


Currently I am passing it like this:



Points is vector of Point objects
tempArr is an empty vector



mergeSort(points, tempArr, points.begin(), points.end(), Point::CompareXCoordinate::operator())


I get an error of C3867: non-standard syntax, use & to create pointer to member



Is that the proper way to pass a comparator or is it a different syntax?










share|improve this question



















  • 1





    See the implemenation of merge sort here. 1) Better to use iterators, not full blown container types such as vector -- this makes it much more flexible. 2) Make the comparison a template argument and pass an instance of the comparison object (if necessary).

    – PaulMcKenzie
    Mar 9 at 1:21












  • Pass an instance of the CompareXCoordinate class.

    – πάντα ῥεῖ
    Mar 9 at 1:22

















0















I have a function in a sorting header for mergeSort



This is my code:



template



void mergeSort(vector<Comparable> & a, vector<Comparable> & tmpArray, int left int right, Comparator cmp)

if (cmp(left,right))

int center = (left + right) / 2;
mergeSort(a, tmpArray, left, center);
mergeSort(a, tmpArray, center + 1, right);
merge(a, tmpArray, left, center + 1, right);




I want to use this comparator and pass in the parameters in my mainDriver.cpp



class CompareXCoordinate 
public:
bool operator()(const Point & p1, const Point & p2) const

return (p1.getX() < p2.getX());

;


Currently I am passing it like this:



Points is vector of Point objects
tempArr is an empty vector



mergeSort(points, tempArr, points.begin(), points.end(), Point::CompareXCoordinate::operator())


I get an error of C3867: non-standard syntax, use & to create pointer to member



Is that the proper way to pass a comparator or is it a different syntax?










share|improve this question



















  • 1





    See the implemenation of merge sort here. 1) Better to use iterators, not full blown container types such as vector -- this makes it much more flexible. 2) Make the comparison a template argument and pass an instance of the comparison object (if necessary).

    – PaulMcKenzie
    Mar 9 at 1:21












  • Pass an instance of the CompareXCoordinate class.

    – πάντα ῥεῖ
    Mar 9 at 1:22













0












0








0








I have a function in a sorting header for mergeSort



This is my code:



template



void mergeSort(vector<Comparable> & a, vector<Comparable> & tmpArray, int left int right, Comparator cmp)

if (cmp(left,right))

int center = (left + right) / 2;
mergeSort(a, tmpArray, left, center);
mergeSort(a, tmpArray, center + 1, right);
merge(a, tmpArray, left, center + 1, right);




I want to use this comparator and pass in the parameters in my mainDriver.cpp



class CompareXCoordinate 
public:
bool operator()(const Point & p1, const Point & p2) const

return (p1.getX() < p2.getX());

;


Currently I am passing it like this:



Points is vector of Point objects
tempArr is an empty vector



mergeSort(points, tempArr, points.begin(), points.end(), Point::CompareXCoordinate::operator())


I get an error of C3867: non-standard syntax, use & to create pointer to member



Is that the proper way to pass a comparator or is it a different syntax?










share|improve this question
















I have a function in a sorting header for mergeSort



This is my code:



template



void mergeSort(vector<Comparable> & a, vector<Comparable> & tmpArray, int left int right, Comparator cmp)

if (cmp(left,right))

int center = (left + right) / 2;
mergeSort(a, tmpArray, left, center);
mergeSort(a, tmpArray, center + 1, right);
merge(a, tmpArray, left, center + 1, right);




I want to use this comparator and pass in the parameters in my mainDriver.cpp



class CompareXCoordinate 
public:
bool operator()(const Point & p1, const Point & p2) const

return (p1.getX() < p2.getX());

;


Currently I am passing it like this:



Points is vector of Point objects
tempArr is an empty vector



mergeSort(points, tempArr, points.begin(), points.end(), Point::CompareXCoordinate::operator())


I get an error of C3867: non-standard syntax, use & to create pointer to member



Is that the proper way to pass a comparator or is it a different syntax?







c++ mergesort cartesian-coordinates






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 1:21









πάντα ῥεῖ

73.8k1077144




73.8k1077144










asked Mar 9 at 1:15









Amor DiazAmor Diaz

164




164







  • 1





    See the implemenation of merge sort here. 1) Better to use iterators, not full blown container types such as vector -- this makes it much more flexible. 2) Make the comparison a template argument and pass an instance of the comparison object (if necessary).

    – PaulMcKenzie
    Mar 9 at 1:21












  • Pass an instance of the CompareXCoordinate class.

    – πάντα ῥεῖ
    Mar 9 at 1:22












  • 1





    See the implemenation of merge sort here. 1) Better to use iterators, not full blown container types such as vector -- this makes it much more flexible. 2) Make the comparison a template argument and pass an instance of the comparison object (if necessary).

    – PaulMcKenzie
    Mar 9 at 1:21












  • Pass an instance of the CompareXCoordinate class.

    – πάντα ῥεῖ
    Mar 9 at 1:22







1




1





See the implemenation of merge sort here. 1) Better to use iterators, not full blown container types such as vector -- this makes it much more flexible. 2) Make the comparison a template argument and pass an instance of the comparison object (if necessary).

– PaulMcKenzie
Mar 9 at 1:21






See the implemenation of merge sort here. 1) Better to use iterators, not full blown container types such as vector -- this makes it much more flexible. 2) Make the comparison a template argument and pass an instance of the comparison object (if necessary).

– PaulMcKenzie
Mar 9 at 1:21














Pass an instance of the CompareXCoordinate class.

– πάντα ῥεῖ
Mar 9 at 1:22





Pass an instance of the CompareXCoordinate class.

– πάντα ῥεῖ
Mar 9 at 1:22












1 Answer
1






active

oldest

votes


















1














operator() is an instance method, so you need to pass an instance of the CompareXCoordinate class as the comparator, not the operator() itself:



mergeSort(points, tempArr, points.begin(), points.end(), Point::CompareXCoordinate());


Though, your class does not act on any non-static data members (that would make sense if you wanted the use of < or > to be configurable), so you could just use a standalone function instead of a class:



bool CompareXCoordinate(const Point & p1, const Point & p2)

return (p1.getX() < p2.getX());


mergeSort(points, tempArr, points.begin(), points.end(), Point::CompareXCoordinate);


Or, if you are using C++11 or later, you can use a lambda instead:



mergeSort(points, tempArr, points.begin(), points.end(),
[](const Point & p1, const Point & p2)
return (p1.getX() < p2.getX());

);





share|improve this answer

























  • No need for (…) around the return expr.

    – Marcelo Cantos
    Mar 9 at 1:36











  • @MarceloCantos technically, yes, but that is a matter of personal style.

    – Remy Lebeau
    Mar 9 at 1:36












  • Sure, but why introduce text that takes more effort and adds nothing to readability?

    – Marcelo Cantos
    Mar 9 at 2:22












  • @MarceloCantos it may not add readability for you, but it can for other people. If you don't want to use it, that is your choice. Other people can choose differently.

    – Remy Lebeau
    Mar 9 at 5:29












  • It doesn't add readability for anyone. Return is a statement. Its expression will never appear in a context requiring precedence disambiguation.

    – Marcelo Cantos
    Mar 9 at 23:20











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073057%2fhow-to-pass-a-comparator-as-a-parameter-for-a-function%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














operator() is an instance method, so you need to pass an instance of the CompareXCoordinate class as the comparator, not the operator() itself:



mergeSort(points, tempArr, points.begin(), points.end(), Point::CompareXCoordinate());


Though, your class does not act on any non-static data members (that would make sense if you wanted the use of < or > to be configurable), so you could just use a standalone function instead of a class:



bool CompareXCoordinate(const Point & p1, const Point & p2)

return (p1.getX() < p2.getX());


mergeSort(points, tempArr, points.begin(), points.end(), Point::CompareXCoordinate);


Or, if you are using C++11 or later, you can use a lambda instead:



mergeSort(points, tempArr, points.begin(), points.end(),
[](const Point & p1, const Point & p2)
return (p1.getX() < p2.getX());

);





share|improve this answer

























  • No need for (…) around the return expr.

    – Marcelo Cantos
    Mar 9 at 1:36











  • @MarceloCantos technically, yes, but that is a matter of personal style.

    – Remy Lebeau
    Mar 9 at 1:36












  • Sure, but why introduce text that takes more effort and adds nothing to readability?

    – Marcelo Cantos
    Mar 9 at 2:22












  • @MarceloCantos it may not add readability for you, but it can for other people. If you don't want to use it, that is your choice. Other people can choose differently.

    – Remy Lebeau
    Mar 9 at 5:29












  • It doesn't add readability for anyone. Return is a statement. Its expression will never appear in a context requiring precedence disambiguation.

    – Marcelo Cantos
    Mar 9 at 23:20















1














operator() is an instance method, so you need to pass an instance of the CompareXCoordinate class as the comparator, not the operator() itself:



mergeSort(points, tempArr, points.begin(), points.end(), Point::CompareXCoordinate());


Though, your class does not act on any non-static data members (that would make sense if you wanted the use of < or > to be configurable), so you could just use a standalone function instead of a class:



bool CompareXCoordinate(const Point & p1, const Point & p2)

return (p1.getX() < p2.getX());


mergeSort(points, tempArr, points.begin(), points.end(), Point::CompareXCoordinate);


Or, if you are using C++11 or later, you can use a lambda instead:



mergeSort(points, tempArr, points.begin(), points.end(),
[](const Point & p1, const Point & p2)
return (p1.getX() < p2.getX());

);





share|improve this answer

























  • No need for (…) around the return expr.

    – Marcelo Cantos
    Mar 9 at 1:36











  • @MarceloCantos technically, yes, but that is a matter of personal style.

    – Remy Lebeau
    Mar 9 at 1:36












  • Sure, but why introduce text that takes more effort and adds nothing to readability?

    – Marcelo Cantos
    Mar 9 at 2:22












  • @MarceloCantos it may not add readability for you, but it can for other people. If you don't want to use it, that is your choice. Other people can choose differently.

    – Remy Lebeau
    Mar 9 at 5:29












  • It doesn't add readability for anyone. Return is a statement. Its expression will never appear in a context requiring precedence disambiguation.

    – Marcelo Cantos
    Mar 9 at 23:20













1












1








1







operator() is an instance method, so you need to pass an instance of the CompareXCoordinate class as the comparator, not the operator() itself:



mergeSort(points, tempArr, points.begin(), points.end(), Point::CompareXCoordinate());


Though, your class does not act on any non-static data members (that would make sense if you wanted the use of < or > to be configurable), so you could just use a standalone function instead of a class:



bool CompareXCoordinate(const Point & p1, const Point & p2)

return (p1.getX() < p2.getX());


mergeSort(points, tempArr, points.begin(), points.end(), Point::CompareXCoordinate);


Or, if you are using C++11 or later, you can use a lambda instead:



mergeSort(points, tempArr, points.begin(), points.end(),
[](const Point & p1, const Point & p2)
return (p1.getX() < p2.getX());

);





share|improve this answer















operator() is an instance method, so you need to pass an instance of the CompareXCoordinate class as the comparator, not the operator() itself:



mergeSort(points, tempArr, points.begin(), points.end(), Point::CompareXCoordinate());


Though, your class does not act on any non-static data members (that would make sense if you wanted the use of < or > to be configurable), so you could just use a standalone function instead of a class:



bool CompareXCoordinate(const Point & p1, const Point & p2)

return (p1.getX() < p2.getX());


mergeSort(points, tempArr, points.begin(), points.end(), Point::CompareXCoordinate);


Or, if you are using C++11 or later, you can use a lambda instead:



mergeSort(points, tempArr, points.begin(), points.end(),
[](const Point & p1, const Point & p2)
return (p1.getX() < p2.getX());

);






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 9 at 1:36

























answered Mar 9 at 1:33









Remy LebeauRemy Lebeau

342k19268461




342k19268461












  • No need for (…) around the return expr.

    – Marcelo Cantos
    Mar 9 at 1:36











  • @MarceloCantos technically, yes, but that is a matter of personal style.

    – Remy Lebeau
    Mar 9 at 1:36












  • Sure, but why introduce text that takes more effort and adds nothing to readability?

    – Marcelo Cantos
    Mar 9 at 2:22












  • @MarceloCantos it may not add readability for you, but it can for other people. If you don't want to use it, that is your choice. Other people can choose differently.

    – Remy Lebeau
    Mar 9 at 5:29












  • It doesn't add readability for anyone. Return is a statement. Its expression will never appear in a context requiring precedence disambiguation.

    – Marcelo Cantos
    Mar 9 at 23:20

















  • No need for (…) around the return expr.

    – Marcelo Cantos
    Mar 9 at 1:36











  • @MarceloCantos technically, yes, but that is a matter of personal style.

    – Remy Lebeau
    Mar 9 at 1:36












  • Sure, but why introduce text that takes more effort and adds nothing to readability?

    – Marcelo Cantos
    Mar 9 at 2:22












  • @MarceloCantos it may not add readability for you, but it can for other people. If you don't want to use it, that is your choice. Other people can choose differently.

    – Remy Lebeau
    Mar 9 at 5:29












  • It doesn't add readability for anyone. Return is a statement. Its expression will never appear in a context requiring precedence disambiguation.

    – Marcelo Cantos
    Mar 9 at 23:20
















No need for (…) around the return expr.

– Marcelo Cantos
Mar 9 at 1:36





No need for (…) around the return expr.

– Marcelo Cantos
Mar 9 at 1:36













@MarceloCantos technically, yes, but that is a matter of personal style.

– Remy Lebeau
Mar 9 at 1:36






@MarceloCantos technically, yes, but that is a matter of personal style.

– Remy Lebeau
Mar 9 at 1:36














Sure, but why introduce text that takes more effort and adds nothing to readability?

– Marcelo Cantos
Mar 9 at 2:22






Sure, but why introduce text that takes more effort and adds nothing to readability?

– Marcelo Cantos
Mar 9 at 2:22














@MarceloCantos it may not add readability for you, but it can for other people. If you don't want to use it, that is your choice. Other people can choose differently.

– Remy Lebeau
Mar 9 at 5:29






@MarceloCantos it may not add readability for you, but it can for other people. If you don't want to use it, that is your choice. Other people can choose differently.

– Remy Lebeau
Mar 9 at 5:29














It doesn't add readability for anyone. Return is a statement. Its expression will never appear in a context requiring precedence disambiguation.

– Marcelo Cantos
Mar 9 at 23:20





It doesn't add readability for anyone. Return is a statement. Its expression will never appear in a context requiring precedence disambiguation.

– Marcelo Cantos
Mar 9 at 23:20



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073057%2fhow-to-pass-a-comparator-as-a-parameter-for-a-function%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme