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;
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
add a comment |
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
1
See the implemenation of merge sort here. 1) Better to use iterators, not full blown container types such asvector
-- 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 theCompareXCoordinate
class.
– πάντα ῥεῖ
Mar 9 at 1:22
add a comment |
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
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
c++ mergesort cartesian-coordinates
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 asvector
-- 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 theCompareXCoordinate
class.
– πάντα ῥεῖ
Mar 9 at 1:22
add a comment |
1
See the implemenation of merge sort here. 1) Better to use iterators, not full blown container types such asvector
-- 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 theCompareXCoordinate
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
add a comment |
1 Answer
1
active
oldest
votes
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());
);
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
|
show 2 more comments
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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());
);
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
|
show 2 more comments
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());
);
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
|
show 2 more comments
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());
);
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());
);
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
|
show 2 more comments
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
|
show 2 more comments
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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