ok, i know i want to erase element number n, however, i do not "know the iterator" for element n.
?????Code:vector<int *> a; .... //'a' filled with 20 values. a.erase(15); //gives syntax error //so does a.erase(a[15]);
ok, i know i want to erase element number n, however, i do not "know the iterator" for element n.
?????Code:vector<int *> a; .... //'a' filled with 20 values. a.erase(15); //gives syntax error //so does a.erase(a[15]);
i seem to have GCC 3.3.4
But how do i start it?
I dont have a menu for it or anything.
What if you use:
a.erase(a.begin() + 15);
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Originally Posted by laserlight
that would make too much sense
how about
a.erase((vector<int *>::iterator) &a[15])
i seem to have GCC 3.3.4
But how do i start it?
I dont have a menu for it or anything.
[edit] My post came reaaally late.Code:a.erase( a.begin() + 15 );
That shouldn't work.how about
a.erase((vector<int *>::iterator) &a[15])
Last edited by Dante Shamest; 03-27-2005 at 02:01 AM.
i don't see why not. it works fine (so it seems). an iterator is nothing but a pointer. so i grab the & of a[n], tell the erase function that it's an iterator, and BAM! - it compiles.Originally Posted by Dante Shamest
if i remember correctly 'iterator' is declared as
Code:template<type T, ......> class vector { typedef T * iterator; }
but this is of course why i ask.
i seem to have GCC 3.3.4
But how do i start it?
I dont have a menu for it or anything.
I apologise. I thought it wouldn't work because I mistakenly assumed you were working with a vector of ints, instead of a vector of int*s.i don't see why not. it works fine (so it seems). an iterator is nothing but a pointer. so i grab the & of a[n], tell the erase function that it's an iterator, and BAM! - it compiles.
Last edited by Dante Shamest; 03-27-2005 at 02:39 AM.
But it's something of a kluge, in my opinion.i don't see why not. it works fine (so it seems). an iterator is nothing but a pointer. so i grab the & of a[n], tell the erase function that it's an iterator, and BAM! - it compiles.
You're going from location to pointer then cast to iterator where you can just directly simulate pointer arithmetic with the iterator.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
works just the same
Code:#include <iostream> #include <stdlib.h> #include <vector> using namespace std; int main(int argc, char *argv[]) { vector<int> a; int i; for(i = 0; i < 10; i++) a.push_back(i); for(i = 0; i < 10; i++) cout << a[i] << ' '; cout << endl; a.erase((vector<int>::iterator) &a[5]); for(i = 0; i < 9; i++) cout << a[i] << ' '; cout << endl; system("PAUSE"); return 0; }
i seem to have GCC 3.3.4
But how do i start it?
I dont have a menu for it or anything.
It works, but is it guaranteed to work? Does the standard guarantee that a vector iterator is a pointer or is this an implementation detail? Calling erase on an item in the middle or beginning of a vector is an expensive operation, as all subsequent elements must be moved. If you need to add and remove elements from the beginning or middle of a container, you should consider another container type.
Originally Posted by anonytmouse
i'm not arguing that my ingenius, super hackish way of erasing an element is the correct way. hell, look at the thread title. i am saying that it DOES work. i would not go into a job interview and do it that way, that's for sure.
a vector is fine. i only need to erase a pointer here and there. it won't happen more than, on average, about 0.5 times per program execution and it only will contain about 6 (pointer) elements tops. i'm not to concerned with it's efficiency.
i seem to have GCC 3.3.4
But how do i start it?
I dont have a menu for it or anything.
Vector supplies random access iterators, therefore begin() + n is defined, and constant time. if begin() is a pointer then it is identical to misplaced hack. In the new draft standard, and all the implementations I know aboutis a pointer to v.size() contiguously allocated ValueT's However, even though pointers and iterators do exactly the same things there is no requrement for there to be a well defined conversion from pointer to iterator. ValueT *p = &(*it); where it is a valid iterator should be kosher, but going the other way is not. Consider, for example, debug iterators that invalidate themselves whenever capacity() is called with a large value.Code:static_cast<std::vector<ValueT>::value_type *>(&v[0])
As a side node std::swap(v[n],v.back()); v.pop_back(); will get rid of element n, in constant (very fast) time, but does not preserve order. Naturally v[n] = v.back(); v.pop_back(); works as well and is faster for types that may not have specialised std::swap.