Is this a valid used of STL vector::erase()

Discussion in 'C++' started by kaede, Nov 23, 2003.

  1. kaede

    kaede Guest

    Hi all,

    I recently came across the following code:

    // some data
    struct Data { // ... some data };

    // a list to hold the data
    vector<Data> dataList;

    // a function to erase an element inside the datalist
    void destroyDataAtIndex(int index)
    {
    dataList.erase( &dataList[index] ); //(a)
    }

    Is line (a) valid? If so, how does that work since the function
    declaration of erase is erase(iterator iter)? Does it automatically
    converts the address of the element to an iterator? or it makes the
    assumption that an iterator is a typedef of pointer?

    Also, I created a toy program with the above code snipplet, from the
    test runs, some times the program will crashed. Is it because of the
    way we used erase in (a)?

    Thanks,
    Kaede
     
    kaede, Nov 23, 2003
    #1
    1. Advertising

  2. On 22 Nov 2003 21:16:45 -0800, (kaede) wrote:

    >Hi all,
    >
    >I recently came across the following code:
    >
    >// some data
    >struct Data { // ... some data };
    >
    >// a list to hold the data
    >vector<Data> dataList;
    >
    >// a function to erase an element inside the datalist
    >void destroyDataAtIndex(int index)
    >{
    > dataList.erase( &dataList[index] ); //(a)
    >}
    >
    >Is line (a) valid? If so, how does that work since the function
    >declaration of erase is erase(iterator iter)? Does it automatically
    >converts the address of the element to an iterator? or it makes the
    >assumption that an iterator is a typedef of pointer?


    No, it's not valid. However, it might work on some compilers that use
    pointers as iterators for vectors. It will fail on others.

    --
    Be seeing you.
     
    Thore Karlsen, Nov 23, 2003
    #2
    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. mosfet

    STL vector and erase : HELP

    mosfet, Nov 14, 2003, in forum: C++
    Replies:
    2
    Views:
    610
    Ivan Vecerina
    Nov 14, 2003
  2. Replies:
    8
    Views:
    1,962
    Csaba
    Feb 18, 2006
  3. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    372
    Pete Becker
    Mar 30, 2006
  4. Anil
    Replies:
    5
    Views:
    446
    Jim Langston
    Dec 18, 2007
  5. Boltar
    Replies:
    6
    Views:
    2,262
    Jerry Coffin
    Jan 5, 2008
Loading...

Share This Page