Thread: vector.erase(pointer?!?!);

  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,897
    >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