Is there a nice way to assign std::minmax(a, b) to std::tie(a, b)?Concatenating two std::vectorsWhat's the best way to trim std::string?How to convert a std::string to const char* or char*?std::wstring VS std::stringHow do I erase an element from std::vector<> by index?Why is “using namespace std;” considered bad practice?What is the easiest way to initialize a std::vector with hardcoded elements?std::string formatting like sprintfreturn statement in lambda expressionEasiest way to convert int to string in C++
I hit a pipe with a mower and now it won't turn
"Plugged in" or "Plugged in in"
Is there reliable evidence that depleted uranium from the 1999 NATO bombing is causing cancer in Serbia?
What is "oversubscription" in Networking?
Plotting the gradient descent
How was film developed in the late 1920s?
Should I report a leak of confidential HR information?
Can I ask to speak to my future colleagues before accepting an offer?
Why transcripts instead of degree certificates?
What is a macro? Difference between macro and function?
Acceleration in Circular motion
How to expand abbrevs without hitting another extra key?
Can I travel from Germany to England alone as an unaccompanied minor?
How did researchers use to find articles before the Internet and the computer era?
Can Access Fault Exceptions of the MC68040 caused by internal access faults occur in normal situations?
Mean Value Theorem: Continuous or Defined?
One folder two different locations on ubuntu 18.04
What does grep -v "grep" mean and do?
In native German words, is Q always followed by U, as in English?
Does Anosov geodesic flow imply asphericity?
Did Wakanda officially get the stuff out of Bucky's head?
Is this hogweed?
3D nonogram, beginner's edition
Why isn’t the tax system continuous rather than bracketed?
Is there a nice way to assign std::minmax(a, b) to std::tie(a, b)?
Concatenating two std::vectorsWhat's the best way to trim std::string?How to convert a std::string to const char* or char*?std::wstring VS std::stringHow do I erase an element from std::vector<> by index?Why is “using namespace std;” considered bad practice?What is the easiest way to initialize a std::vector with hardcoded elements?std::string formatting like sprintfreturn statement in lambda expressionEasiest way to convert int to string in C++
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
std::tie(a, b) = std::minmax(a, b);
I think this is intuitive code. Clean and understandable. Too bad it doesn't work as intended, as std::minmax
templates for const&
. If therefore the values are swapped inside the std::pair<const&, const&>
than one assignement will overwrite the other value:
auto[a, b] = std::make_pair(7, 5);
std::tie(a, b) = std::minmax(a, b);
std::cout << "a: " << a << ", b: " << b << 'n';
a: 5, b: 5
The expected output here is a: 5, b: 7
.
I think this is important as implementing transform functions to apply a function onto some ranges requires such statements for intuitive lambdas. For example:
std::vector<int> v 0, 1, 0, 2, 0 ;
std::vector<int> u 1, 0, 1, 0, 1 ;
perform(v.begin(), v.end(), u.begin(), [](auto& a, auto& b)
std::tie(a, b) = std::minmax(a, b);
);
//v would be == 0, 0, 0, 0, 0
//u would be == 1, 1, 1, 2, 1
One solution I found was constructing a std::tuple
explicitly without any reference qualifiers over the std::pair<const&, const&>
to enforce a copy:
std::tie(a, b) = std::tuple<int, int>(std::minmax(a, b));
But this <int, int>
redundancy seems rather awful, especially when having saidauto& a, auto& b
before.
Is there a nice, short way to perform this assignement? Could it be that this is the wrong direction and just saying if (a >= b) std::swap(a, b);
would be the best approach here?
c++ algorithm reference stl c++17
add a comment |
std::tie(a, b) = std::minmax(a, b);
I think this is intuitive code. Clean and understandable. Too bad it doesn't work as intended, as std::minmax
templates for const&
. If therefore the values are swapped inside the std::pair<const&, const&>
than one assignement will overwrite the other value:
auto[a, b] = std::make_pair(7, 5);
std::tie(a, b) = std::minmax(a, b);
std::cout << "a: " << a << ", b: " << b << 'n';
a: 5, b: 5
The expected output here is a: 5, b: 7
.
I think this is important as implementing transform functions to apply a function onto some ranges requires such statements for intuitive lambdas. For example:
std::vector<int> v 0, 1, 0, 2, 0 ;
std::vector<int> u 1, 0, 1, 0, 1 ;
perform(v.begin(), v.end(), u.begin(), [](auto& a, auto& b)
std::tie(a, b) = std::minmax(a, b);
);
//v would be == 0, 0, 0, 0, 0
//u would be == 1, 1, 1, 2, 1
One solution I found was constructing a std::tuple
explicitly without any reference qualifiers over the std::pair<const&, const&>
to enforce a copy:
std::tie(a, b) = std::tuple<int, int>(std::minmax(a, b));
But this <int, int>
redundancy seems rather awful, especially when having saidauto& a, auto& b
before.
Is there a nice, short way to perform this assignement? Could it be that this is the wrong direction and just saying if (a >= b) std::swap(a, b);
would be the best approach here?
c++ algorithm reference stl c++17
While the answers provided are nice, I'm somewhat unhappy about the temporaries, as these might get costly with some data types. So I personally would rather go with theif-swap
approach. You might pack it in your ownminmax_inplace
template function (with void return type)...
– Aconcagua
5 hours ago
add a comment |
std::tie(a, b) = std::minmax(a, b);
I think this is intuitive code. Clean and understandable. Too bad it doesn't work as intended, as std::minmax
templates for const&
. If therefore the values are swapped inside the std::pair<const&, const&>
than one assignement will overwrite the other value:
auto[a, b] = std::make_pair(7, 5);
std::tie(a, b) = std::minmax(a, b);
std::cout << "a: " << a << ", b: " << b << 'n';
a: 5, b: 5
The expected output here is a: 5, b: 7
.
I think this is important as implementing transform functions to apply a function onto some ranges requires such statements for intuitive lambdas. For example:
std::vector<int> v 0, 1, 0, 2, 0 ;
std::vector<int> u 1, 0, 1, 0, 1 ;
perform(v.begin(), v.end(), u.begin(), [](auto& a, auto& b)
std::tie(a, b) = std::minmax(a, b);
);
//v would be == 0, 0, 0, 0, 0
//u would be == 1, 1, 1, 2, 1
One solution I found was constructing a std::tuple
explicitly without any reference qualifiers over the std::pair<const&, const&>
to enforce a copy:
std::tie(a, b) = std::tuple<int, int>(std::minmax(a, b));
But this <int, int>
redundancy seems rather awful, especially when having saidauto& a, auto& b
before.
Is there a nice, short way to perform this assignement? Could it be that this is the wrong direction and just saying if (a >= b) std::swap(a, b);
would be the best approach here?
c++ algorithm reference stl c++17
std::tie(a, b) = std::minmax(a, b);
I think this is intuitive code. Clean and understandable. Too bad it doesn't work as intended, as std::minmax
templates for const&
. If therefore the values are swapped inside the std::pair<const&, const&>
than one assignement will overwrite the other value:
auto[a, b] = std::make_pair(7, 5);
std::tie(a, b) = std::minmax(a, b);
std::cout << "a: " << a << ", b: " << b << 'n';
a: 5, b: 5
The expected output here is a: 5, b: 7
.
I think this is important as implementing transform functions to apply a function onto some ranges requires such statements for intuitive lambdas. For example:
std::vector<int> v 0, 1, 0, 2, 0 ;
std::vector<int> u 1, 0, 1, 0, 1 ;
perform(v.begin(), v.end(), u.begin(), [](auto& a, auto& b)
std::tie(a, b) = std::minmax(a, b);
);
//v would be == 0, 0, 0, 0, 0
//u would be == 1, 1, 1, 2, 1
One solution I found was constructing a std::tuple
explicitly without any reference qualifiers over the std::pair<const&, const&>
to enforce a copy:
std::tie(a, b) = std::tuple<int, int>(std::minmax(a, b));
But this <int, int>
redundancy seems rather awful, especially when having saidauto& a, auto& b
before.
Is there a nice, short way to perform this assignement? Could it be that this is the wrong direction and just saying if (a >= b) std::swap(a, b);
would be the best approach here?
c++ algorithm reference stl c++17
c++ algorithm reference stl c++17
edited 2 hours ago
Stack Danny
asked 8 hours ago
Stack DannyStack Danny
2,6319 silver badges36 bronze badges
2,6319 silver badges36 bronze badges
While the answers provided are nice, I'm somewhat unhappy about the temporaries, as these might get costly with some data types. So I personally would rather go with theif-swap
approach. You might pack it in your ownminmax_inplace
template function (with void return type)...
– Aconcagua
5 hours ago
add a comment |
While the answers provided are nice, I'm somewhat unhappy about the temporaries, as these might get costly with some data types. So I personally would rather go with theif-swap
approach. You might pack it in your ownminmax_inplace
template function (with void return type)...
– Aconcagua
5 hours ago
While the answers provided are nice, I'm somewhat unhappy about the temporaries, as these might get costly with some data types. So I personally would rather go with the
if-swap
approach. You might pack it in your own minmax_inplace
template function (with void return type)...– Aconcagua
5 hours ago
While the answers provided are nice, I'm somewhat unhappy about the temporaries, as these might get costly with some data types. So I personally would rather go with the
if-swap
approach. You might pack it in your own minmax_inplace
template function (with void return type)...– Aconcagua
5 hours ago
add a comment |
2 Answers
2
active
oldest
votes
You can use an initializer list for minmax
:
std::tie(a, b) = std::minmax(a, b);
This causes temporary objects to be created (just like when using unary +) but it has the benefit that it works with types that is lacking unary +.
using namespace std::string_view_literals;
auto [a, b] = std::make_pair("foo"sv, "bar"sv);
std::tie(a, b) = std::minmax(a, b);
std::cout << "a: " << a << ", b: " << b << 'n';
Output:
a: bar, b: foo
4
This has the nice benefit that it works for more types as well (can't unary-+
astd::string
, for instance).
– Barry
8 hours ago
4
You may want to note that this causes the creation of temporary objects so it could be expensive.
– NathanOliver
8 hours ago
1
@NathanOliver Well, you need temporaries. Can't really get around that? Problem is that this does copies instead of moves because ofinitializer_list
... Can't get around that either :-(
– Barry
8 hours ago
1
@Barry Well, if the OP is fine with a custom function doing a swap and returning a pair of references, or not even returning anything, would bypass any temporary creation.
– NathanOliver
8 hours ago
2
I agree, this is the shortest, least confusing and most maintainable approach.
– Stack Danny
7 hours ago
|
show 1 more comment
You can enforce this with a certain level of brevity as follows.
std::tie(a, b) = std::minmax(+a, +b);
std::cout << "a: " << a << ", b: " << b << 'n';
Explanation: the builtin unary plus operator, for the sake of symmetry with its unary minus sibling, returns its operand by value (it also performs the usual arithmetic conversions, but that doesn't apply to int
s). This means it has to create a temporary, even though this temporary is nothing but a copy of the operand. But for the usage of minmax
in this example, it's sufficient: swapping references here doesn't assign through anymore, because the references on the right hand side (the const int&
arguments passed to minmax
) don't refer to the same objects as those on the left hand side (inside the tuple
of references created by std::tie
).
The output is as desired:
a: 5, b: 7
3
Great minds think alike or something...
– Quentin
8 hours ago
That's very clever. Thanks
– Stack Danny
8 hours ago
6
some explanation would be helpful
– Hariom Singh
8 hours ago
3
You may want to note that this causes the creation of temporary objects so it could be expensive.
– NathanOliver
8 hours ago
@HariomSingh You are right, and I'm sorry it took me so long to get back to this thread. I hope it's clearer now.
– lubgr
4 hours ago
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%2f56739747%2fis-there-a-nice-way-to-assign-stdminmaxa-b-to-stdtiea-b%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use an initializer list for minmax
:
std::tie(a, b) = std::minmax(a, b);
This causes temporary objects to be created (just like when using unary +) but it has the benefit that it works with types that is lacking unary +.
using namespace std::string_view_literals;
auto [a, b] = std::make_pair("foo"sv, "bar"sv);
std::tie(a, b) = std::minmax(a, b);
std::cout << "a: " << a << ", b: " << b << 'n';
Output:
a: bar, b: foo
4
This has the nice benefit that it works for more types as well (can't unary-+
astd::string
, for instance).
– Barry
8 hours ago
4
You may want to note that this causes the creation of temporary objects so it could be expensive.
– NathanOliver
8 hours ago
1
@NathanOliver Well, you need temporaries. Can't really get around that? Problem is that this does copies instead of moves because ofinitializer_list
... Can't get around that either :-(
– Barry
8 hours ago
1
@Barry Well, if the OP is fine with a custom function doing a swap and returning a pair of references, or not even returning anything, would bypass any temporary creation.
– NathanOliver
8 hours ago
2
I agree, this is the shortest, least confusing and most maintainable approach.
– Stack Danny
7 hours ago
|
show 1 more comment
You can use an initializer list for minmax
:
std::tie(a, b) = std::minmax(a, b);
This causes temporary objects to be created (just like when using unary +) but it has the benefit that it works with types that is lacking unary +.
using namespace std::string_view_literals;
auto [a, b] = std::make_pair("foo"sv, "bar"sv);
std::tie(a, b) = std::minmax(a, b);
std::cout << "a: " << a << ", b: " << b << 'n';
Output:
a: bar, b: foo
4
This has the nice benefit that it works for more types as well (can't unary-+
astd::string
, for instance).
– Barry
8 hours ago
4
You may want to note that this causes the creation of temporary objects so it could be expensive.
– NathanOliver
8 hours ago
1
@NathanOliver Well, you need temporaries. Can't really get around that? Problem is that this does copies instead of moves because ofinitializer_list
... Can't get around that either :-(
– Barry
8 hours ago
1
@Barry Well, if the OP is fine with a custom function doing a swap and returning a pair of references, or not even returning anything, would bypass any temporary creation.
– NathanOliver
8 hours ago
2
I agree, this is the shortest, least confusing and most maintainable approach.
– Stack Danny
7 hours ago
|
show 1 more comment
You can use an initializer list for minmax
:
std::tie(a, b) = std::minmax(a, b);
This causes temporary objects to be created (just like when using unary +) but it has the benefit that it works with types that is lacking unary +.
using namespace std::string_view_literals;
auto [a, b] = std::make_pair("foo"sv, "bar"sv);
std::tie(a, b) = std::minmax(a, b);
std::cout << "a: " << a << ", b: " << b << 'n';
Output:
a: bar, b: foo
You can use an initializer list for minmax
:
std::tie(a, b) = std::minmax(a, b);
This causes temporary objects to be created (just like when using unary +) but it has the benefit that it works with types that is lacking unary +.
using namespace std::string_view_literals;
auto [a, b] = std::make_pair("foo"sv, "bar"sv);
std::tie(a, b) = std::minmax(a, b);
std::cout << "a: " << a << ", b: " << b << 'n';
Output:
a: bar, b: foo
edited 8 hours ago
answered 8 hours ago
Ted LyngmoTed Lyngmo
5,4702 gold badges7 silver badges24 bronze badges
5,4702 gold badges7 silver badges24 bronze badges
4
This has the nice benefit that it works for more types as well (can't unary-+
astd::string
, for instance).
– Barry
8 hours ago
4
You may want to note that this causes the creation of temporary objects so it could be expensive.
– NathanOliver
8 hours ago
1
@NathanOliver Well, you need temporaries. Can't really get around that? Problem is that this does copies instead of moves because ofinitializer_list
... Can't get around that either :-(
– Barry
8 hours ago
1
@Barry Well, if the OP is fine with a custom function doing a swap and returning a pair of references, or not even returning anything, would bypass any temporary creation.
– NathanOliver
8 hours ago
2
I agree, this is the shortest, least confusing and most maintainable approach.
– Stack Danny
7 hours ago
|
show 1 more comment
4
This has the nice benefit that it works for more types as well (can't unary-+
astd::string
, for instance).
– Barry
8 hours ago
4
You may want to note that this causes the creation of temporary objects so it could be expensive.
– NathanOliver
8 hours ago
1
@NathanOliver Well, you need temporaries. Can't really get around that? Problem is that this does copies instead of moves because ofinitializer_list
... Can't get around that either :-(
– Barry
8 hours ago
1
@Barry Well, if the OP is fine with a custom function doing a swap and returning a pair of references, or not even returning anything, would bypass any temporary creation.
– NathanOliver
8 hours ago
2
I agree, this is the shortest, least confusing and most maintainable approach.
– Stack Danny
7 hours ago
4
4
This has the nice benefit that it works for more types as well (can't unary-
+
a std::string
, for instance).– Barry
8 hours ago
This has the nice benefit that it works for more types as well (can't unary-
+
a std::string
, for instance).– Barry
8 hours ago
4
4
You may want to note that this causes the creation of temporary objects so it could be expensive.
– NathanOliver
8 hours ago
You may want to note that this causes the creation of temporary objects so it could be expensive.
– NathanOliver
8 hours ago
1
1
@NathanOliver Well, you need temporaries. Can't really get around that? Problem is that this does copies instead of moves because of
initializer_list
... Can't get around that either :-(– Barry
8 hours ago
@NathanOliver Well, you need temporaries. Can't really get around that? Problem is that this does copies instead of moves because of
initializer_list
... Can't get around that either :-(– Barry
8 hours ago
1
1
@Barry Well, if the OP is fine with a custom function doing a swap and returning a pair of references, or not even returning anything, would bypass any temporary creation.
– NathanOliver
8 hours ago
@Barry Well, if the OP is fine with a custom function doing a swap and returning a pair of references, or not even returning anything, would bypass any temporary creation.
– NathanOliver
8 hours ago
2
2
I agree, this is the shortest, least confusing and most maintainable approach.
– Stack Danny
7 hours ago
I agree, this is the shortest, least confusing and most maintainable approach.
– Stack Danny
7 hours ago
|
show 1 more comment
You can enforce this with a certain level of brevity as follows.
std::tie(a, b) = std::minmax(+a, +b);
std::cout << "a: " << a << ", b: " << b << 'n';
Explanation: the builtin unary plus operator, for the sake of symmetry with its unary minus sibling, returns its operand by value (it also performs the usual arithmetic conversions, but that doesn't apply to int
s). This means it has to create a temporary, even though this temporary is nothing but a copy of the operand. But for the usage of minmax
in this example, it's sufficient: swapping references here doesn't assign through anymore, because the references on the right hand side (the const int&
arguments passed to minmax
) don't refer to the same objects as those on the left hand side (inside the tuple
of references created by std::tie
).
The output is as desired:
a: 5, b: 7
3
Great minds think alike or something...
– Quentin
8 hours ago
That's very clever. Thanks
– Stack Danny
8 hours ago
6
some explanation would be helpful
– Hariom Singh
8 hours ago
3
You may want to note that this causes the creation of temporary objects so it could be expensive.
– NathanOliver
8 hours ago
@HariomSingh You are right, and I'm sorry it took me so long to get back to this thread. I hope it's clearer now.
– lubgr
4 hours ago
add a comment |
You can enforce this with a certain level of brevity as follows.
std::tie(a, b) = std::minmax(+a, +b);
std::cout << "a: " << a << ", b: " << b << 'n';
Explanation: the builtin unary plus operator, for the sake of symmetry with its unary minus sibling, returns its operand by value (it also performs the usual arithmetic conversions, but that doesn't apply to int
s). This means it has to create a temporary, even though this temporary is nothing but a copy of the operand. But for the usage of minmax
in this example, it's sufficient: swapping references here doesn't assign through anymore, because the references on the right hand side (the const int&
arguments passed to minmax
) don't refer to the same objects as those on the left hand side (inside the tuple
of references created by std::tie
).
The output is as desired:
a: 5, b: 7
3
Great minds think alike or something...
– Quentin
8 hours ago
That's very clever. Thanks
– Stack Danny
8 hours ago
6
some explanation would be helpful
– Hariom Singh
8 hours ago
3
You may want to note that this causes the creation of temporary objects so it could be expensive.
– NathanOliver
8 hours ago
@HariomSingh You are right, and I'm sorry it took me so long to get back to this thread. I hope it's clearer now.
– lubgr
4 hours ago
add a comment |
You can enforce this with a certain level of brevity as follows.
std::tie(a, b) = std::minmax(+a, +b);
std::cout << "a: " << a << ", b: " << b << 'n';
Explanation: the builtin unary plus operator, for the sake of symmetry with its unary minus sibling, returns its operand by value (it also performs the usual arithmetic conversions, but that doesn't apply to int
s). This means it has to create a temporary, even though this temporary is nothing but a copy of the operand. But for the usage of minmax
in this example, it's sufficient: swapping references here doesn't assign through anymore, because the references on the right hand side (the const int&
arguments passed to minmax
) don't refer to the same objects as those on the left hand side (inside the tuple
of references created by std::tie
).
The output is as desired:
a: 5, b: 7
You can enforce this with a certain level of brevity as follows.
std::tie(a, b) = std::minmax(+a, +b);
std::cout << "a: " << a << ", b: " << b << 'n';
Explanation: the builtin unary plus operator, for the sake of symmetry with its unary minus sibling, returns its operand by value (it also performs the usual arithmetic conversions, but that doesn't apply to int
s). This means it has to create a temporary, even though this temporary is nothing but a copy of the operand. But for the usage of minmax
in this example, it's sufficient: swapping references here doesn't assign through anymore, because the references on the right hand side (the const int&
arguments passed to minmax
) don't refer to the same objects as those on the left hand side (inside the tuple
of references created by std::tie
).
The output is as desired:
a: 5, b: 7
edited 4 hours ago
answered 8 hours ago
lubgrlubgr
20k3 gold badges30 silver badges69 bronze badges
20k3 gold badges30 silver badges69 bronze badges
3
Great minds think alike or something...
– Quentin
8 hours ago
That's very clever. Thanks
– Stack Danny
8 hours ago
6
some explanation would be helpful
– Hariom Singh
8 hours ago
3
You may want to note that this causes the creation of temporary objects so it could be expensive.
– NathanOliver
8 hours ago
@HariomSingh You are right, and I'm sorry it took me so long to get back to this thread. I hope it's clearer now.
– lubgr
4 hours ago
add a comment |
3
Great minds think alike or something...
– Quentin
8 hours ago
That's very clever. Thanks
– Stack Danny
8 hours ago
6
some explanation would be helpful
– Hariom Singh
8 hours ago
3
You may want to note that this causes the creation of temporary objects so it could be expensive.
– NathanOliver
8 hours ago
@HariomSingh You are right, and I'm sorry it took me so long to get back to this thread. I hope it's clearer now.
– lubgr
4 hours ago
3
3
Great minds think alike or something...
– Quentin
8 hours ago
Great minds think alike or something...
– Quentin
8 hours ago
That's very clever. Thanks
– Stack Danny
8 hours ago
That's very clever. Thanks
– Stack Danny
8 hours ago
6
6
some explanation would be helpful
– Hariom Singh
8 hours ago
some explanation would be helpful
– Hariom Singh
8 hours ago
3
3
You may want to note that this causes the creation of temporary objects so it could be expensive.
– NathanOliver
8 hours ago
You may want to note that this causes the creation of temporary objects so it could be expensive.
– NathanOliver
8 hours ago
@HariomSingh You are right, and I'm sorry it took me so long to get back to this thread. I hope it's clearer now.
– lubgr
4 hours ago
@HariomSingh You are right, and I'm sorry it took me so long to get back to this thread. I hope it's clearer now.
– lubgr
4 hours ago
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%2f56739747%2fis-there-a-nice-way-to-assign-stdminmaxa-b-to-stdtiea-b%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
While the answers provided are nice, I'm somewhat unhappy about the temporaries, as these might get costly with some data types. So I personally would rather go with the
if-swap
approach. You might pack it in your ownminmax_inplace
template function (with void return type)...– Aconcagua
5 hours ago