A
awm129
I seem to be confused. I understand the implications of erasing an
element in the middle of vector and what that means for existing
iterators, but I'm not sure where this behavior is coming from.
Consider the following program and output:
#include <iostream>
#include <ostream>
#include <vector>
using namespace std;
class Element
{
public:
Element() : m_var(1){};
~Element();
int m_var;
};
inline ostream& operator<<( ostream& ss, const Element& e )
{
return ss << "0x" << &e.m_var;
}
inline ostream& operator<<( ostream& ss, const
vector<Element>::iterator& e )
{
return ss << *e;
}
Element::~Element()
{
cout << " deleting: " << *this << endl;
}
int main()
{
vector<Element> vec;
vector<Element>::iterator itr;
// add three elements
itr = vec.insert( vec.end(), Element() );
cout << "added: " << itr << endl;
itr = vec.insert( vec.end(), Element() );
cout << "added: " << itr << endl;
itr = vec.insert( vec.end(), Element() );
cout << "added: " << itr << endl;
// print them all out
for( itr = vec.begin(); itr != vec.end(); ++itr )
{
cout << "in the vector: " << itr << endl;
}
// remove the middle Element
// the erase seems to be removing the wrong element...
itr = vec.begin() + 1;
cout << "erasing: " << itr << endl;
vec.erase( itr );
return 0;
}
OUTPUT:
deleting: 0x0012FD90
deleting: 0x0012FF28
added: 0x003660B0
deleting: 0x003660B0
deleting: 0x0012FD90
deleting: 0x0012FF14
added: 0x0036618C
deleting: 0x00366188
deleting: 0x0036618C
deleting: 0x0012FD90
deleting: 0x0012FF00
added: 0x003661D8
in the vector: 0x003661D0
in the vector: 0x003661D4
in the vector: 0x003661D8
erasing: 0x003661D4
deleting: 0x003661D8
Shouldn't the last two lines be the same memory address?! Its like
erase is removing the element AFTER the iterator I'm giving it. I
feel really dumb, what am I missing here?
I intend to rewrite this using stl algorithm (remove_if and friends)
but I need to understand what is going on here first. Thanks!
element in the middle of vector and what that means for existing
iterators, but I'm not sure where this behavior is coming from.
Consider the following program and output:
#include <iostream>
#include <ostream>
#include <vector>
using namespace std;
class Element
{
public:
Element() : m_var(1){};
~Element();
int m_var;
};
inline ostream& operator<<( ostream& ss, const Element& e )
{
return ss << "0x" << &e.m_var;
}
inline ostream& operator<<( ostream& ss, const
vector<Element>::iterator& e )
{
return ss << *e;
}
Element::~Element()
{
cout << " deleting: " << *this << endl;
}
int main()
{
vector<Element> vec;
vector<Element>::iterator itr;
// add three elements
itr = vec.insert( vec.end(), Element() );
cout << "added: " << itr << endl;
itr = vec.insert( vec.end(), Element() );
cout << "added: " << itr << endl;
itr = vec.insert( vec.end(), Element() );
cout << "added: " << itr << endl;
// print them all out
for( itr = vec.begin(); itr != vec.end(); ++itr )
{
cout << "in the vector: " << itr << endl;
}
// remove the middle Element
// the erase seems to be removing the wrong element...
itr = vec.begin() + 1;
cout << "erasing: " << itr << endl;
vec.erase( itr );
return 0;
}
OUTPUT:
deleting: 0x0012FD90
deleting: 0x0012FF28
added: 0x003660B0
deleting: 0x003660B0
deleting: 0x0012FD90
deleting: 0x0012FF14
added: 0x0036618C
deleting: 0x00366188
deleting: 0x0036618C
deleting: 0x0012FD90
deleting: 0x0012FF00
added: 0x003661D8
in the vector: 0x003661D0
in the vector: 0x003661D4
in the vector: 0x003661D8
erasing: 0x003661D4
deleting: 0x003661D8
Shouldn't the last two lines be the same memory address?! Its like
erase is removing the element AFTER the iterator I'm giving it. I
feel really dumb, what am I missing here?
I intend to rewrite this using stl algorithm (remove_if and friends)
but I need to understand what is going on here first. Thanks!