vector.erase(pointer?!?!);

This is a discussion on vector.erase(pointer?!?!); within the C++ Programming forums, part of the General Programming Boards category; why won't this compile?... Code: void main() { vector<int> v; //add 100 elements to a v.erase(4); //or v.erase(non-pointervariables) } but ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    vector.erase(pointer?!?!);

    why won't this compile?...

    Code:
    void main()
    {
       vector<int> v;
       //add 100 elements to a
       v.erase(4);   //or v.erase(non-pointervariables)
    }
    but this will

    Code:
    void main()
    {
       vector<int> v;
       int *i;
       *i = 5;
       //add 100 elements to a
       v.erase(i);
    }

    it just doesn't make any sense to me why it will only take a pointer?

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    19

    Smile

    It doesn't work because vector.erase() takes an iterator as argument.
    In case 2 it only works because in this case the iterator is a pointer. But this mustn't always be true. So you should always use an iterator as argument and nothing else

    mfg JJ

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    ohhhhhhhhhhhhhh..........

    i never read up on iterators......

    that makes sense....thanks alot

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >In case 2 it only works because in this case the iterator is a pointer.
    Actually, it only works out of sheer dumb luck.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21