Does the STL have a way to apply a function before calling less than?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
A lot of algorithms accept a comparison object. Often, I end up with something like
std::sort(begin, end, [&](auto const& lhs, auto const& rhs) {
return Function(lhs) < Function(rhs);
});
Is there anything in the STL to apply a Function before calling less than? So I could write:
std::sort(begin, end, std::DoesThisExist(Function));
I know I could write my own, but I wonder if this already exists. I glanced through cpprefence but didn't see it. Could easily have missed it.
c++
New contributor
add a comment |
A lot of algorithms accept a comparison object. Often, I end up with something like
std::sort(begin, end, [&](auto const& lhs, auto const& rhs) {
return Function(lhs) < Function(rhs);
});
Is there anything in the STL to apply a Function before calling less than? So I could write:
std::sort(begin, end, std::DoesThisExist(Function));
I know I could write my own, but I wonder if this already exists. I glanced through cpprefence but didn't see it. Could easily have missed it.
c++
New contributor
I don't think this exists, however, it should be very easy to write.
– JVApen
4 hours ago
add a comment |
A lot of algorithms accept a comparison object. Often, I end up with something like
std::sort(begin, end, [&](auto const& lhs, auto const& rhs) {
return Function(lhs) < Function(rhs);
});
Is there anything in the STL to apply a Function before calling less than? So I could write:
std::sort(begin, end, std::DoesThisExist(Function));
I know I could write my own, but I wonder if this already exists. I glanced through cpprefence but didn't see it. Could easily have missed it.
c++
New contributor
A lot of algorithms accept a comparison object. Often, I end up with something like
std::sort(begin, end, [&](auto const& lhs, auto const& rhs) {
return Function(lhs) < Function(rhs);
});
Is there anything in the STL to apply a Function before calling less than? So I could write:
std::sort(begin, end, std::DoesThisExist(Function));
I know I could write my own, but I wonder if this already exists. I glanced through cpprefence but didn't see it. Could easily have missed it.
c++
c++
New contributor
New contributor
New contributor
asked 6 hours ago
Fomar putesFomar putes
483
483
New contributor
New contributor
I don't think this exists, however, it should be very easy to write.
– JVApen
4 hours ago
add a comment |
I don't think this exists, however, it should be very easy to write.
– JVApen
4 hours ago
I don't think this exists, however, it should be very easy to write.
– JVApen
4 hours ago
I don't think this exists, however, it should be very easy to write.
– JVApen
4 hours ago
add a comment |
2 Answers
2
active
oldest
votes
The STL
should have a sort that works on a transform of the elements rather than the elements themselves. The reason for this being that Function
could actually be costly. By simply incorporating it into the comparison as you did you invoke Function nlog(n)
times rather than the optimal n.
To sort arrays in parallel using STL algorithm :
std::sort(std::execution::par, container.begin(), container.end(), comparison_object);
Anyway, I think if you try to just sort with ranges::view::transform
it will probably still call your function ~n log n many times. But you could just do something like:
auto values = /* some container */;
auto keys = values | ranges::view::transform(f) | ranges::to_vector;
ranges::sort(ranges::view::zip(keys, values),
(auto const& x, auto const& y) { return std::get<0>(x) < std::get<0>(y); });
Calling a transform n log n times matters only if its expense is significant compared to the comparison and permutation. In common cases where the “transform” is just selecting a member or performing simple arithmetic, allocating the space for the key values would be much worse than the repeated work.
– Davis Herring
2 hours ago
add a comment |
The Ranges TS (which has been merged for C++20) defines variations of many of the standard algorithms that include projections with exactly this behavior.
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
});
}
});
Fomar putes is a new contributor. Be nice, and check out our Code of Conduct.
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%2f55678360%2fdoes-the-stl-have-a-way-to-apply-a-function-before-calling-less-than%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
The STL
should have a sort that works on a transform of the elements rather than the elements themselves. The reason for this being that Function
could actually be costly. By simply incorporating it into the comparison as you did you invoke Function nlog(n)
times rather than the optimal n.
To sort arrays in parallel using STL algorithm :
std::sort(std::execution::par, container.begin(), container.end(), comparison_object);
Anyway, I think if you try to just sort with ranges::view::transform
it will probably still call your function ~n log n many times. But you could just do something like:
auto values = /* some container */;
auto keys = values | ranges::view::transform(f) | ranges::to_vector;
ranges::sort(ranges::view::zip(keys, values),
(auto const& x, auto const& y) { return std::get<0>(x) < std::get<0>(y); });
Calling a transform n log n times matters only if its expense is significant compared to the comparison and permutation. In common cases where the “transform” is just selecting a member or performing simple arithmetic, allocating the space for the key values would be much worse than the repeated work.
– Davis Herring
2 hours ago
add a comment |
The STL
should have a sort that works on a transform of the elements rather than the elements themselves. The reason for this being that Function
could actually be costly. By simply incorporating it into the comparison as you did you invoke Function nlog(n)
times rather than the optimal n.
To sort arrays in parallel using STL algorithm :
std::sort(std::execution::par, container.begin(), container.end(), comparison_object);
Anyway, I think if you try to just sort with ranges::view::transform
it will probably still call your function ~n log n many times. But you could just do something like:
auto values = /* some container */;
auto keys = values | ranges::view::transform(f) | ranges::to_vector;
ranges::sort(ranges::view::zip(keys, values),
(auto const& x, auto const& y) { return std::get<0>(x) < std::get<0>(y); });
Calling a transform n log n times matters only if its expense is significant compared to the comparison and permutation. In common cases where the “transform” is just selecting a member or performing simple arithmetic, allocating the space for the key values would be much worse than the repeated work.
– Davis Herring
2 hours ago
add a comment |
The STL
should have a sort that works on a transform of the elements rather than the elements themselves. The reason for this being that Function
could actually be costly. By simply incorporating it into the comparison as you did you invoke Function nlog(n)
times rather than the optimal n.
To sort arrays in parallel using STL algorithm :
std::sort(std::execution::par, container.begin(), container.end(), comparison_object);
Anyway, I think if you try to just sort with ranges::view::transform
it will probably still call your function ~n log n many times. But you could just do something like:
auto values = /* some container */;
auto keys = values | ranges::view::transform(f) | ranges::to_vector;
ranges::sort(ranges::view::zip(keys, values),
(auto const& x, auto const& y) { return std::get<0>(x) < std::get<0>(y); });
The STL
should have a sort that works on a transform of the elements rather than the elements themselves. The reason for this being that Function
could actually be costly. By simply incorporating it into the comparison as you did you invoke Function nlog(n)
times rather than the optimal n.
To sort arrays in parallel using STL algorithm :
std::sort(std::execution::par, container.begin(), container.end(), comparison_object);
Anyway, I think if you try to just sort with ranges::view::transform
it will probably still call your function ~n log n many times. But you could just do something like:
auto values = /* some container */;
auto keys = values | ranges::view::transform(f) | ranges::to_vector;
ranges::sort(ranges::view::zip(keys, values),
(auto const& x, auto const& y) { return std::get<0>(x) < std::get<0>(y); });
answered 4 hours ago
Ben Chaliah AyoubBen Chaliah Ayoub
2,422317
2,422317
Calling a transform n log n times matters only if its expense is significant compared to the comparison and permutation. In common cases where the “transform” is just selecting a member or performing simple arithmetic, allocating the space for the key values would be much worse than the repeated work.
– Davis Herring
2 hours ago
add a comment |
Calling a transform n log n times matters only if its expense is significant compared to the comparison and permutation. In common cases where the “transform” is just selecting a member or performing simple arithmetic, allocating the space for the key values would be much worse than the repeated work.
– Davis Herring
2 hours ago
Calling a transform n log n times matters only if its expense is significant compared to the comparison and permutation. In common cases where the “transform” is just selecting a member or performing simple arithmetic, allocating the space for the key values would be much worse than the repeated work.
– Davis Herring
2 hours ago
Calling a transform n log n times matters only if its expense is significant compared to the comparison and permutation. In common cases where the “transform” is just selecting a member or performing simple arithmetic, allocating the space for the key values would be much worse than the repeated work.
– Davis Herring
2 hours ago
add a comment |
The Ranges TS (which has been merged for C++20) defines variations of many of the standard algorithms that include projections with exactly this behavior.
add a comment |
The Ranges TS (which has been merged for C++20) defines variations of many of the standard algorithms that include projections with exactly this behavior.
add a comment |
The Ranges TS (which has been merged for C++20) defines variations of many of the standard algorithms that include projections with exactly this behavior.
The Ranges TS (which has been merged for C++20) defines variations of many of the standard algorithms that include projections with exactly this behavior.
answered 6 hours ago
Davis HerringDavis Herring
9,1001736
9,1001736
add a comment |
add a comment |
Fomar putes is a new contributor. Be nice, and check out our Code of Conduct.
Fomar putes is a new contributor. Be nice, and check out our Code of Conduct.
Fomar putes is a new contributor. Be nice, and check out our Code of Conduct.
Fomar putes is a new contributor. Be nice, and check out our Code of Conduct.
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%2f55678360%2fdoes-the-stl-have-a-way-to-apply-a-function-before-calling-less-than%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
I don't think this exists, however, it should be very easy to write.
– JVApen
4 hours ago