operator overloading in a struct in cpp The 2019 Stack Overflow Developer Survey Results Are InWhy doesn't Java offer operator overloading?What is the “-->” operator in C++?What are the basic rules and idioms for operator overloading?Operator overloading : member function vs. non-member function?overloading operator !=Comparison operator overloading for a struct, symmetrically comparing my struct with an int type?Operator overloading in C++ (operator-)Operator Overloading for struct pointersOverload Struct OperatorsDefining two-argument operator overload for private struct
Right tool to dig six foot holes?
Is an up-to-date browser secure on an out-of-date OS?
How to save as into a customized destination on macOS?
Button changing it's text & action. Good or terrible?
How to type this arrow in math mode?
Origin of "cooter" meaning "vagina"
How to notate time signature switching consistently every measure
The difference between dialogue marks
What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?
During Temple times, who can butcher a kosher animal?
Why isn't airport relocation done gradually?
Why was M87 targetted for the Event Horizon Telescope instead of Sagittarius A*?
Why do we hear so much about the Trump administration deciding to impose and then remove tariffs?
Return to UK after being refused entry years previously
Who coined the term "madman theory"?
When should I buy a clipper card after flying to OAK?
Why didn't the Event Horizon Telescope team mention Sagittarius A*?
Is "plugging out" electronic devices an American expression?
Why isn't the circumferential light around the M87 black hole's event horizon symmetric?
What to do when moving next to a bird sanctuary with a loosely-domesticated cat?
Earliest use of the term "Galois extension"?
Deal with toxic manager when you can't quit
Write faster on AT24C32
Can a rogue use sneak attack with weapons that have the thrown property even if they are not thrown?
operator overloading in a struct in cpp
The 2019 Stack Overflow Developer Survey Results Are InWhy doesn't Java offer operator overloading?What is the “-->” operator in C++?What are the basic rules and idioms for operator overloading?Operator overloading : member function vs. non-member function?overloading operator !=Comparison operator overloading for a struct, symmetrically comparing my struct with an int type?Operator overloading in C++ (operator-)Operator Overloading for struct pointersOverload Struct OperatorsDefining two-argument operator overload for private struct
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I was reading and I am confused in below operator overloading part,
here how we can sort without defining the comparator.
How is sorting taking Place over two structure without defining any comparator,how compiler get to know on what basis it has two sort?
When we are going to use when e1
suppose the default comparator of STL sort using something like this
e1bool operator<(Edge const& other) inside struct.
struct Edge
int u, v, weight;
bool operator<(Edge const& other)
return weight < other.weight;
;
vector<Edge> edges;
sort(edges.begin(), edges.end());
c++ operator-overloading
add a comment |
I was reading and I am confused in below operator overloading part,
here how we can sort without defining the comparator.
How is sorting taking Place over two structure without defining any comparator,how compiler get to know on what basis it has two sort?
When we are going to use when e1
suppose the default comparator of STL sort using something like this
e1bool operator<(Edge const& other) inside struct.
struct Edge
int u, v, weight;
bool operator<(Edge const& other)
return weight < other.weight;
;
vector<Edge> edges;
sort(edges.begin(), edges.end());
c++ operator-overloading
2
This is really unclear you know? Please edit your question before it gets closed. Be sure to actually ask a question. Also, read How to Ask.
– YSC
Mar 8 at 9:35
the question is still unclear. "how we can sort without defining the comparator." but you do define a comparator. As you correctly suppose,sort
usesoperator<
as default. It reads as if you answer your own question
– user463035818
Mar 8 at 11:05
Are you asking howstd::sort
works?
– Max Langhof
Mar 8 at 11:43
add a comment |
I was reading and I am confused in below operator overloading part,
here how we can sort without defining the comparator.
How is sorting taking Place over two structure without defining any comparator,how compiler get to know on what basis it has two sort?
When we are going to use when e1
suppose the default comparator of STL sort using something like this
e1bool operator<(Edge const& other) inside struct.
struct Edge
int u, v, weight;
bool operator<(Edge const& other)
return weight < other.weight;
;
vector<Edge> edges;
sort(edges.begin(), edges.end());
c++ operator-overloading
I was reading and I am confused in below operator overloading part,
here how we can sort without defining the comparator.
How is sorting taking Place over two structure without defining any comparator,how compiler get to know on what basis it has two sort?
When we are going to use when e1
suppose the default comparator of STL sort using something like this
e1bool operator<(Edge const& other) inside struct.
struct Edge
int u, v, weight;
bool operator<(Edge const& other)
return weight < other.weight;
;
vector<Edge> edges;
sort(edges.begin(), edges.end());
c++ operator-overloading
c++ operator-overloading
edited Mar 8 at 10:44
humblefool
asked Mar 8 at 9:33
humblefoolhumblefool
61
61
2
This is really unclear you know? Please edit your question before it gets closed. Be sure to actually ask a question. Also, read How to Ask.
– YSC
Mar 8 at 9:35
the question is still unclear. "how we can sort without defining the comparator." but you do define a comparator. As you correctly suppose,sort
usesoperator<
as default. It reads as if you answer your own question
– user463035818
Mar 8 at 11:05
Are you asking howstd::sort
works?
– Max Langhof
Mar 8 at 11:43
add a comment |
2
This is really unclear you know? Please edit your question before it gets closed. Be sure to actually ask a question. Also, read How to Ask.
– YSC
Mar 8 at 9:35
the question is still unclear. "how we can sort without defining the comparator." but you do define a comparator. As you correctly suppose,sort
usesoperator<
as default. It reads as if you answer your own question
– user463035818
Mar 8 at 11:05
Are you asking howstd::sort
works?
– Max Langhof
Mar 8 at 11:43
2
2
This is really unclear you know? Please edit your question before it gets closed. Be sure to actually ask a question. Also, read How to Ask.
– YSC
Mar 8 at 9:35
This is really unclear you know? Please edit your question before it gets closed. Be sure to actually ask a question. Also, read How to Ask.
– YSC
Mar 8 at 9:35
the question is still unclear. "how we can sort without defining the comparator." but you do define a comparator. As you correctly suppose,
sort
uses operator<
as default. It reads as if you answer your own question– user463035818
Mar 8 at 11:05
the question is still unclear. "how we can sort without defining the comparator." but you do define a comparator. As you correctly suppose,
sort
uses operator<
as default. It reads as if you answer your own question– user463035818
Mar 8 at 11:05
Are you asking how
std::sort
works?– Max Langhof
Mar 8 at 11:43
Are you asking how
std::sort
works?– Max Langhof
Mar 8 at 11:43
add a comment |
1 Answer
1
active
oldest
votes
By default, std::sort()
uses operator<()
as a comparator. This is why sorting works with your Edge
struct.
If you wish to perform custom sorting, or your struct/class doesn't provide operator<()
, then you would need to pass a comparator function object to std::sort()
. For example, if we change your Edge
struct to remove operator<()
...
struct Edge
int u, v, weight;
;
void print_edges(const std::vector<Edge> &edges)
for (Edge const &e : edges)
std::cout << e.weight << std::endl;
int main()
std::vector<Edge> edges 4, 5, 9, 1, 2, 3 ;
std::cout << "Unsorted:n";
print_edges(edges);
std::sort(edges.begin(), edges.end(), [](Edge const &lhs, Edge const &rhs)
return lhs.weight < rhs.weight;
);
std::cout << "Sorted:n";
print_edges(edges);
return 0;
can you tell me how the sorting taking place in above algorithm I mean you have defined your own comparator but they did something in structure?
– humblefool
Mar 8 at 9:56
@humblefool - I updated my answer to discuss howstd::sort()
works without a custom comparator at the start.
– Karl Nicoll
Mar 8 at 10:41
add a comment |
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%2f55060335%2foperator-overloading-in-a-struct-in-cpp%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
By default, std::sort()
uses operator<()
as a comparator. This is why sorting works with your Edge
struct.
If you wish to perform custom sorting, or your struct/class doesn't provide operator<()
, then you would need to pass a comparator function object to std::sort()
. For example, if we change your Edge
struct to remove operator<()
...
struct Edge
int u, v, weight;
;
void print_edges(const std::vector<Edge> &edges)
for (Edge const &e : edges)
std::cout << e.weight << std::endl;
int main()
std::vector<Edge> edges 4, 5, 9, 1, 2, 3 ;
std::cout << "Unsorted:n";
print_edges(edges);
std::sort(edges.begin(), edges.end(), [](Edge const &lhs, Edge const &rhs)
return lhs.weight < rhs.weight;
);
std::cout << "Sorted:n";
print_edges(edges);
return 0;
can you tell me how the sorting taking place in above algorithm I mean you have defined your own comparator but they did something in structure?
– humblefool
Mar 8 at 9:56
@humblefool - I updated my answer to discuss howstd::sort()
works without a custom comparator at the start.
– Karl Nicoll
Mar 8 at 10:41
add a comment |
By default, std::sort()
uses operator<()
as a comparator. This is why sorting works with your Edge
struct.
If you wish to perform custom sorting, or your struct/class doesn't provide operator<()
, then you would need to pass a comparator function object to std::sort()
. For example, if we change your Edge
struct to remove operator<()
...
struct Edge
int u, v, weight;
;
void print_edges(const std::vector<Edge> &edges)
for (Edge const &e : edges)
std::cout << e.weight << std::endl;
int main()
std::vector<Edge> edges 4, 5, 9, 1, 2, 3 ;
std::cout << "Unsorted:n";
print_edges(edges);
std::sort(edges.begin(), edges.end(), [](Edge const &lhs, Edge const &rhs)
return lhs.weight < rhs.weight;
);
std::cout << "Sorted:n";
print_edges(edges);
return 0;
can you tell me how the sorting taking place in above algorithm I mean you have defined your own comparator but they did something in structure?
– humblefool
Mar 8 at 9:56
@humblefool - I updated my answer to discuss howstd::sort()
works without a custom comparator at the start.
– Karl Nicoll
Mar 8 at 10:41
add a comment |
By default, std::sort()
uses operator<()
as a comparator. This is why sorting works with your Edge
struct.
If you wish to perform custom sorting, or your struct/class doesn't provide operator<()
, then you would need to pass a comparator function object to std::sort()
. For example, if we change your Edge
struct to remove operator<()
...
struct Edge
int u, v, weight;
;
void print_edges(const std::vector<Edge> &edges)
for (Edge const &e : edges)
std::cout << e.weight << std::endl;
int main()
std::vector<Edge> edges 4, 5, 9, 1, 2, 3 ;
std::cout << "Unsorted:n";
print_edges(edges);
std::sort(edges.begin(), edges.end(), [](Edge const &lhs, Edge const &rhs)
return lhs.weight < rhs.weight;
);
std::cout << "Sorted:n";
print_edges(edges);
return 0;
By default, std::sort()
uses operator<()
as a comparator. This is why sorting works with your Edge
struct.
If you wish to perform custom sorting, or your struct/class doesn't provide operator<()
, then you would need to pass a comparator function object to std::sort()
. For example, if we change your Edge
struct to remove operator<()
...
struct Edge
int u, v, weight;
;
void print_edges(const std::vector<Edge> &edges)
for (Edge const &e : edges)
std::cout << e.weight << std::endl;
int main()
std::vector<Edge> edges 4, 5, 9, 1, 2, 3 ;
std::cout << "Unsorted:n";
print_edges(edges);
std::sort(edges.begin(), edges.end(), [](Edge const &lhs, Edge const &rhs)
return lhs.weight < rhs.weight;
);
std::cout << "Sorted:n";
print_edges(edges);
return 0;
edited Mar 8 at 10:38
answered Mar 8 at 9:43
Karl NicollKarl Nicoll
11.8k33956
11.8k33956
can you tell me how the sorting taking place in above algorithm I mean you have defined your own comparator but they did something in structure?
– humblefool
Mar 8 at 9:56
@humblefool - I updated my answer to discuss howstd::sort()
works without a custom comparator at the start.
– Karl Nicoll
Mar 8 at 10:41
add a comment |
can you tell me how the sorting taking place in above algorithm I mean you have defined your own comparator but they did something in structure?
– humblefool
Mar 8 at 9:56
@humblefool - I updated my answer to discuss howstd::sort()
works without a custom comparator at the start.
– Karl Nicoll
Mar 8 at 10:41
can you tell me how the sorting taking place in above algorithm I mean you have defined your own comparator but they did something in structure?
– humblefool
Mar 8 at 9:56
can you tell me how the sorting taking place in above algorithm I mean you have defined your own comparator but they did something in structure?
– humblefool
Mar 8 at 9:56
@humblefool - I updated my answer to discuss how
std::sort()
works without a custom comparator at the start.– Karl Nicoll
Mar 8 at 10:41
@humblefool - I updated my answer to discuss how
std::sort()
works without a custom comparator at the start.– Karl Nicoll
Mar 8 at 10:41
add a comment |
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%2f55060335%2foperator-overloading-in-a-struct-in-cpp%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
2
This is really unclear you know? Please edit your question before it gets closed. Be sure to actually ask a question. Also, read How to Ask.
– YSC
Mar 8 at 9:35
the question is still unclear. "how we can sort without defining the comparator." but you do define a comparator. As you correctly suppose,
sort
usesoperator<
as default. It reads as if you answer your own question– user463035818
Mar 8 at 11:05
Are you asking how
std::sort
works?– Max Langhof
Mar 8 at 11:43