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. C / C++ / MFC
  4. Inserting an element into an ordered vector/list

Inserting an element into an ordered vector/list

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsquestion
8 Posts 4 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.
  • A Offline
    A Offline
    avimitrani
    wrote on last edited by
    #1

    Is it possible to insert an element directly into it's 'right' place in an ordered vector, instead of using push_back() and then sort()? If not, is it possible with a list? Thanks, Avi.

    Z S J 3 Replies Last reply
    0
    • A avimitrani

      Is it possible to insert an element directly into it's 'right' place in an ordered vector, instead of using push_back() and then sort()? If not, is it possible with a list? Thanks, Avi.

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

      You can use insert, but you have to march through the vector/list to find the correct location to place it. Alternatively, you can use the set template which will always be sorted (but requires uniqueness) or multiset (which will be sorted and stores non-unique items in a vector at each location).

      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

      A 1 Reply Last reply
      0
      • Z Zac Howland

        You can use insert, but you have to march through the vector/list to find the correct location to place it. Alternatively, you can use the set template which will always be sorted (but requires uniqueness) or multiset (which will be sorted and stores non-unique items in a vector at each location).

        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

        A Offline
        A Offline
        avimitrani
        wrote on last edited by
        #3

        Thanks, but maybe it will be better to keep it a vector and use the algorithm find_if() (just foun out about it...) and then insert() Avi.

        Z S 2 Replies Last reply
        0
        • A avimitrani

          Thanks, but maybe it will be better to keep it a vector and use the algorithm find_if() (just foun out about it...) and then insert() Avi.

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

          That all depends on what you are using it for, so whatever solution that works best for what your project needs is the one you should use (just make sure have a reason for picking a particular solution over another).

          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
          • A avimitrani

            Is it possible to insert an element directly into it's 'right' place in an ordered vector, instead of using push_back() and then sort()? If not, is it possible with a list? Thanks, Avi.

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            Use std::lower_bound followed by std::vector::insert. e.g. using namespace std; vector<int> vec; // Fill vector. It's sorted. int newval = 5; vec.insert(lower_bound(vec.begin(), vec.end(), newval), newval);

            Steve

            A 1 Reply Last reply
            0
            • A avimitrani

              Thanks, but maybe it will be better to keep it a vector and use the algorithm find_if() (just foun out about it...) and then insert() Avi.

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #6

              find_if is a bad choice if the vector is sorted as you'll get linear performance instead of logarithmic (for the search). Use std::lower_bound instead.

              Steve

              1 Reply Last reply
              0
              • A avimitrani

                Is it possible to insert an element directly into it's 'right' place in an ordered vector, instead of using push_back() and then sort()? If not, is it possible with a list? Thanks, Avi.

                J Offline
                J Offline
                jhwurmbach
                wrote on last edited by
                #7

                You often need to add or remove from the end? You need it only occational but in time critical situations? You do need the guarantee that your items are stored in adjacent places? (Double-use of &vec[0] as a C-Array for example) Then use the vector/lower_bound Stephen Hewitt proposed If you only need a sorted storage, you would be better off with set/multiset.


                "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.

                1 Reply Last reply
                0
                • S Stephen Hewitt

                  Use std::lower_bound followed by std::vector::insert. e.g. using namespace std; vector<int> vec; // Fill vector. It's sorted. int newval = 5; vec.insert(lower_bound(vec.begin(), vec.end(), newval), newval);

                  Steve

                  A Offline
                  A Offline
                  avimitrani
                  wrote on last edited by
                  #8

                  Thanks Steve, This is exactly what I was looking for. :cool: Avi.

                  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