say I have an vector with
2
5
8
2
and I deleted 5 (first off, how would I delete 5?). How can I then have 8 and 2 shift up so the new vector is
2
8
2
:confused:
Printable View
say I have an vector with
2
5
8
2
and I deleted 5 (first off, how would I delete 5?). How can I then have 8 and 2 shift up so the new vector is
2
8
2
:confused:
You would probably write a vector wrapper:
You would then code the RemoveElement to iterate through the m_vector member, starting at the index to remove. The one above it would shift down one, and overwrite the index to remove. Continue this process until you hit m_vector.size()-1. Then, resize().Code:template<class t>
class shift_vector
{
private:
std::vector<t> m_vector;
public:
shift_vector();
~shift_vector();
void RemoveElement(unsigned int index);
...
};
Z.
yeah...I was wondering if there is a prewritten function for this. Guess not..... :(
I posted the algorithm. You should be able to implement it yourself.
Z.
If you're using the STL, then you can swap vector for deque, and use the pop_front method :)
yeah...
I couldnt find vector.h anywhere so I found it on some school website. It only has 2 functions. Did I get some base version of it?
It's just <vector>, not <vector.h>. The standard c++ library headers have no .h extension.
Or you use a double linked list (<list>). The architecture of a list makes the shifting unnecessary.
That depends on what you want to do with the whole thing though...
BTW, calling vector::erase will automatically shift the elements after the deletion point, but all references to elements after the deletion point become invalid:
Code:vector<int> vi;
// add 10 elements with the value 5
vi.insert(vi.end(), 10, 5);
// get a reference to the 6th element
int &ri = vi[5];
// delete the second element
vector<int>::iterator it = vi.begin();
it++;
vi.erase(it);
// INVALID!!! May produce unforeseeable results!
dosomething(ri);