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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page