Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. STL list and sort

STL list and sort

Scheduled Pinned Locked Moved ATL / WTL / STL
questionc++
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    neilsolent
    wrote on last edited by
    #1

    Gurus, If I have an STL list (doesn't matter what of) and I sort it, how can I add one element to the list, whilst maintaining the sort order? All I can think of to achieve this is to either: (1) add the new element to one end, and run sort() again, or (2) iterate through the entire list, checking the sort predicate against each element. Neither of these seem particulalrly appealing. Does anyone know the most processor efficient way of doing this please ? thanks, Neil.

    cheers, Neil

    L S 2 Replies Last reply
    0
    • N neilsolent

      Gurus, If I have an STL list (doesn't matter what of) and I sort it, how can I add one element to the list, whilst maintaining the sort order? All I can think of to achieve this is to either: (1) add the new element to one end, and run sort() again, or (2) iterate through the entire list, checking the sort predicate against each element. Neither of these seem particulalrly appealing. Does anyone know the most processor efficient way of doing this please ? thanks, Neil.

      cheers, Neil

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      They hide that information in the documentation[^] Lower_bound is a version of binary search: it attempts to find the element value in an ordered range [first, last) [1]. Specifically, it returns the first position where value could be inserted without violating the ordering. However that might not be the "most efficient" for your application. There are several things to consider. If you use STL much you should have Scott Meyers book as it discusses efficiency issues.

      led mike

      N S 2 Replies Last reply
      0
      • N neilsolent

        Gurus, If I have an STL list (doesn't matter what of) and I sort it, how can I add one element to the list, whilst maintaining the sort order? All I can think of to achieve this is to either: (1) add the new element to one end, and run sort() again, or (2) iterate through the entire list, checking the sort predicate against each element. Neither of these seem particulalrly appealing. Does anyone know the most processor efficient way of doing this please ? thanks, Neil.

        cheers, Neil

        S Offline
        S Offline
        Sceptic Mole
        wrote on last edited by
        #3

        neilsolent wrote:

        f I have an STL list (doesn't matter what of) and I sort it, how can I add one element to the list, whilst maintaining the sort order? All I can think of to achieve this is to either: (1) add the new element to one end, and run sort() again, or (2) iterate through the entire list, checking the sort predicate against each element. Neither of these seem particulalrly appealing. Does anyone know the most processor efficient way of doing this please ?

        If you insert only one element (2) is probably faster. You can make it even faster if you know from which end to start.

        N 1 Reply Last reply
        0
        • L led mike

          They hide that information in the documentation[^] Lower_bound is a version of binary search: it attempts to find the element value in an ordered range [first, last) [1]. Specifically, it returns the first position where value could be inserted without violating the ordering. However that might not be the "most efficient" for your application. There are several things to consider. If you use STL much you should have Scott Meyers book as it discusses efficiency issues.

          led mike

          N Offline
          N Offline
          neilsolent
          wrote on last edited by
          #4

          Thanks for that answer, Neil

          cheers, Neil

          1 Reply Last reply
          0
          • S Sceptic Mole

            neilsolent wrote:

            f I have an STL list (doesn't matter what of) and I sort it, how can I add one element to the list, whilst maintaining the sort order? All I can think of to achieve this is to either: (1) add the new element to one end, and run sort() again, or (2) iterate through the entire list, checking the sort predicate against each element. Neither of these seem particulalrly appealing. Does anyone know the most processor efficient way of doing this please ?

            If you insert only one element (2) is probably faster. You can make it even faster if you know from which end to start.

            N Offline
            N Offline
            neilsolent
            wrote on last edited by
            #5

            Thanks for that answer. Unfortunately there will be no way of knowing which end to start from. Neil

            cheers, Neil

            S 1 Reply Last reply
            0
            • N neilsolent

              Thanks for that answer. Unfortunately there will be no way of knowing which end to start from. Neil

              cheers, Neil

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              Best bet is to guess by seeing if it's closer in value to front() or back(). That way, the average search distance for random data will be N/2 where N is the list length.

              S 1 Reply Last reply
              0
              • L led mike

                They hide that information in the documentation[^] Lower_bound is a version of binary search: it attempts to find the element value in an ordered range [first, last) [1]. Specifically, it returns the first position where value could be inserted without violating the ordering. However that might not be the "most efficient" for your application. There are several things to consider. If you use STL much you should have Scott Meyers book as it discusses efficiency issues.

                led mike

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                led mike wrote:

                However that might not be the "most efficient" for your application.

                It probably won't be - I don't think the structure of std::list iterators is terribly conducive to binary searches - could be wrong, mind.

                Z 1 Reply Last reply
                0
                • S Stuart Dootson

                  Best bet is to guess by seeing if it's closer in value to front() or back(). That way, the average search distance for random data will be N/2 where N is the list length.

                  S Offline
                  S Offline
                  Sceptic Mole
                  wrote on last edited by
                  #8

                  Stuart Dootson wrote:

                  Best bet is to guess by seeing if it's closer in value to front() or back(). That way, the average search distance for random data will be N/2 where N is the list length.

                  but only when the values are distributed equally.

                  1 Reply Last reply
                  0
                  • S Stuart Dootson

                    led mike wrote:

                    However that might not be the "most efficient" for your application.

                    It probably won't be - I don't think the structure of std::list iterators is terribly conducive to binary searches - could be wrong, mind.

                    Z Offline
                    Z Offline
                    Zac Howland
                    wrote on last edited by
                    #9

                    Stuart Dootson wrote:

                    It probably won't be - I don't think the structure of std::list iterators is terribly conducive to binary searches - could be wrong, mind.

                    std::deque would probably be a better choice in most cases.

                    If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups