Swapping a single pointer item in vector?

Discussion in 'C++' started by Howard, Dec 5, 2007.

  1. Howard

    Howard Guest

    Hi all,
    is there an easy way to swap one pointer item in a vector with a pointer
    that's not yet in the vector?

    Currently, I'm using begin()+index to get an iterator to the item I want
    to swap out, then deleting the pointer there, then using erase to get rid of
    the vector item itself and get an iterator to the next item, and finally
    using insert to put my new pointer in the correct place. Is there an easier
    way?

    Thanks,
    -Howard
     
    Howard, Dec 5, 2007
    #1
    1. Advertising

  2. * Howard:
    >
    > is there an easy way to swap one pointer item in a vector with a
    > pointer that's not yet in the vector?


    std::swap comes to mind.

    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Dec 5, 2007
    #2
    1. Advertising

  3. Howard

    Jim Langston Guest

    "Howard" <> wrote in message
    news:...
    > Hi all,
    > is there an easy way to swap one pointer item in a vector with a
    > pointer that's not yet in the vector?
    >
    > Currently, I'm using begin()+index to get an iterator to the item I
    > want to swap out, then deleting the pointer there, then using erase to get
    > rid of the vector item itself and get an iterator to the next item, and
    > finally using insert to put my new pointer in the correct place. Is there
    > an easier way?


    Just change it. The output of the following program is
    10 20
    10 10

    #include <iostream>
    #include <vector>

    int main()
    {
    std::vector<int*> MyVect;
    int a = 10;
    int b = 20;

    MyVect.push_back( &a );
    MyVect.push_back( &b );

    for ( std::vector<int*>::iterator it = MyVect.begin(); it !=
    MyVect.end(); ++it )
    std::cout << *(*it) << " ";
    std::cout << "\n";

    std::vector<int*>::iterator it = MyVect.begin() + 1;
    *it = &a;

    for ( std::vector<int*>::iterator it = MyVect.begin(); it !=
    MyVect.end(); ++it )
    std::cout << *(*it) << " ";
    std::cout << "\n";
    }
     
    Jim Langston, Dec 5, 2007
    #3
  4. Howard

    Howard Guest

    ----- Original Message -----
    From: "Jim Langston" <>
    Newsgroups: comp.lang.c++
    Sent: Tuesday, December 04, 2007 7:01 PM
    Subject: Re: Swapping a single pointer item in vector?


    > "Howard" <> wrote in message
    > news:...
    >> Hi all,
    >> is there an easy way to swap one pointer item in a vector with a
    >> pointer that's not yet in the vector?
    >>
    >> Currently, I'm using begin()+index to get an iterator to the item I
    >> want to swap out, then deleting the pointer there, then using erase to
    >> get rid of the vector item itself and get an iterator to the next item,
    >> and finally using insert to put my new pointer in the correct place. Is
    >> there an easier way?

    >
    > Just change it. std::vector<int*>::iterator it = MyVect.begin() + 1;
    > *it = &a;


    D'oh! Of course! :) Thanks, Jim
    -Howard
     
    Howard, Dec 5, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. deancoo
    Replies:
    7
    Views:
    504
    deancoo
    Mar 4, 2005
  2. Replies:
    29
    Views:
    1,085
    Mabden
    Nov 5, 2005
  3. Replies:
    8
    Views:
    1,999
    Csaba
    Feb 18, 2006
  4. Replies:
    5
    Views:
    342
    peter koch
    Jul 21, 2006
  5. QUASAR
    Replies:
    6
    Views:
    460
    QUASAR
    Jan 17, 2004
Loading...

Share This Page