Does the STL have a way to apply a function before calling less than?Does the 'mutable' keyword have any purpose other than allowing the variable to be modified by a const function?Pretty-print C++ STL containersElegant way to find closest value in a vector from aboveLess-than function dereferencing pointersShould custom containers have free begin/end functions?why do std::sort and partial_sort require random-access iterators?c++ Sort Vector based on distance to external PointHow can I avoid “for” loops with an “if” condition inside them with C++?Do STL algorithms functions, like accumulate, avoid copy if the function passed to them accepts a reference?Why does std::sort segfault with non-transitive comparators?

Lead the way to this Literary Knight to its final “DESTINATION”

How can I detect if I'm in a subshell?

Background for black and white chart

How to know whether to write accidentals as sharps or flats?

1960s sci-fi anthology with a Viking fighting a U.S. army MP on the cover

My student in one course asks for paid tutoring in another course. Appropriate?

How to make all magic-casting innate, but still rare?

How to sort human readable size

100-doors puzzle

High-end PC graphics circa 1990?

How can I maintain game balance while allowing my player to craft genuinely useful items?

What is the context for Napoleon's quote "[the Austrians] did not know the value of five minutes"?

Redirecting output only on a successful command call

The instant an accelerating object has zero speed, is it speeding up, slowing down, or neither?

Is there a risk to write an invitation letter for a stranger to obtain a Czech (Schengen) visa?

Leaving job close to major deadlines

Does anyone recognize these rockets, and their location?

Is it a bad idea to have a pen name with only an initial for a surname?

Are their examples of rowers who also fought?

Converting 3x7 to a 1x7. Is it possible with only existing parts?

First occurrence in the Sixers sequence

Have Steve Rogers (Captain America) and a young Erik Lehnsherr (Magneto) interacted during WWII?

Why is Skinner so awkward in Hot Fuzz?

Is the infant mortality rate among African-American babies in Youngstown, Ohio greater than that of babies in Iran?



Does the STL have a way to apply a function before calling less than?


Does the 'mutable' keyword have any purpose other than allowing the variable to be modified by a const function?Pretty-print C++ STL containersElegant way to find closest value in a vector from aboveLess-than function dereferencing pointersShould custom containers have free begin/end functions?why do std::sort and partial_sort require random-access iterators?c++ Sort Vector based on distance to external PointHow can I avoid “for” loops with an “if” condition inside them with C++?Do STL algorithms functions, like accumulate, avoid copy if the function passed to them accepts a reference?Why does std::sort segfault with non-transitive comparators?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








10















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.










share|improve this question






















  • I don't think this exists, however, it should be very easy to write.

    – JVApen
    Apr 14 at 20:03











  • std::bind, see details below

    – Helmut Zeisel
    Apr 16 at 7:24


















10















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.










share|improve this question






















  • I don't think this exists, however, it should be very easy to write.

    – JVApen
    Apr 14 at 20:03











  • std::bind, see details below

    – Helmut Zeisel
    Apr 16 at 7:24














10












10








10








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.










share|improve this question














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++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 14 at 17:59









Fomar putesFomar putes

534




534












  • I don't think this exists, however, it should be very easy to write.

    – JVApen
    Apr 14 at 20:03











  • std::bind, see details below

    – Helmut Zeisel
    Apr 16 at 7:24


















  • I don't think this exists, however, it should be very easy to write.

    – JVApen
    Apr 14 at 20:03











  • std::bind, see details below

    – Helmut Zeisel
    Apr 16 at 7:24

















I don't think this exists, however, it should be very easy to write.

– JVApen
Apr 14 at 20:03





I don't think this exists, however, it should be very easy to write.

– JVApen
Apr 14 at 20:03













std::bind, see details below

– Helmut Zeisel
Apr 16 at 7:24






std::bind, see details below

– Helmut Zeisel
Apr 16 at 7:24













3 Answers
3






active

oldest

votes


















4














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); );





share|improve this answer























  • 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
    Apr 14 at 22:10


















2














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.






share|improve this answer






























    0














    In principle, you could use std::bind, but it is very verbose:



     typedef std::remove_reference<decltype(*begin)>::type T;
    std::sort(begin, end, std::bind(std::less<T>(),
    std::bind(Function,std::placeholders::_1),
    std::bind(Function,std::placeholders::_2)));





    share|improve this answer























      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%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

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      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); );





      share|improve this answer























      • 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
        Apr 14 at 22:10















      4














      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); );





      share|improve this answer























      • 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
        Apr 14 at 22:10













      4












      4








      4







      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); );





      share|improve this answer













      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); );






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Apr 14 at 19:59









      Ben Chaliah AyoubBen Chaliah Ayoub

      4,532422




      4,532422












      • 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
        Apr 14 at 22:10

















      • 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
        Apr 14 at 22:10
















      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
      Apr 14 at 22:10





      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
      Apr 14 at 22:10













      2














      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.






      share|improve this answer



























        2














        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.






        share|improve this answer

























          2












          2








          2







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 14 at 18:11









          Davis HerringDavis Herring

          10.8k1736




          10.8k1736





















              0














              In principle, you could use std::bind, but it is very verbose:



               typedef std::remove_reference<decltype(*begin)>::type T;
              std::sort(begin, end, std::bind(std::less<T>(),
              std::bind(Function,std::placeholders::_1),
              std::bind(Function,std::placeholders::_2)));





              share|improve this answer



























                0














                In principle, you could use std::bind, but it is very verbose:



                 typedef std::remove_reference<decltype(*begin)>::type T;
                std::sort(begin, end, std::bind(std::less<T>(),
                std::bind(Function,std::placeholders::_1),
                std::bind(Function,std::placeholders::_2)));





                share|improve this answer

























                  0












                  0








                  0







                  In principle, you could use std::bind, but it is very verbose:



                   typedef std::remove_reference<decltype(*begin)>::type T;
                  std::sort(begin, end, std::bind(std::less<T>(),
                  std::bind(Function,std::placeholders::_1),
                  std::bind(Function,std::placeholders::_2)));





                  share|improve this answer













                  In principle, you could use std::bind, but it is very verbose:



                   typedef std::remove_reference<decltype(*begin)>::type T;
                  std::sort(begin, end, std::bind(std::less<T>(),
                  std::bind(Function,std::placeholders::_1),
                  std::bind(Function,std::placeholders::_2)));






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 15 at 8:32









                  Helmut ZeiselHelmut Zeisel

                  1387




                  1387



























                      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%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





















































                      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

                      Tamil (spriik) Luke uk diar | Nawigatjuun

                      Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

                      Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?