Inserting an element into an ordered vector/list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • avimitrani
    New Member
    • Sep 2006
    • 7

    Inserting an element into an ordered vector/list

    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.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Yes look up the method

    vector::insert( ...)

    Comment

    • vermarajeev
      New Member
      • Aug 2006
      • 180

      #3
      Originally posted by 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.
      I agree with Banfa, use something like this,

      vector<int> vec;
      vec.insert(vec. begin(), 5); //This will place 5 at the beginning
      vec.insert(vec. begin(), 4); //This will place 4 before 5

      and so on.....

      Comment

      • dtimes6
        New Member
        • Oct 2006
        • 73

        #4
        seehttp://www.sgi.com/tech/stl/index.html

        Comment

        Working...