First you will need to change the interface. Given the two iterators, there is no way to return to the container to which they belong; so if you want to change the container, you have to pass a link to it, that is :.
void GUIComponentText::AddAttributes( std::vector<GUIComponentAttribute*>& attributes ) { for ( std::vector<GUIComponentAttribute*>::iter = attributes.begin(); iter != attributes.end(); ++ iter ) {
By doing this: insertion can invalidate iterators. So it depends on where you want to insert. If you want to insert position: std::vector<>::insert
one element returns an iterator to this element that was inserted before your element, so that you can assign it to your iterator, configure (if necessary) and continue:
iter = attributes.insert(iter, newAttribute); ++ iter; // Return to where we were...
If you add ( push_back
), the problem is a bit more complicated; you need to calculate the offset and then restore the iterator:
size_t offset = iter - attributes.begin(); attributes.push_back( nweAttribute ); iter = attributes.begin() + offset;
In this case, it is probably easier to iterate with size_t
and []
, rather than an iterator.
source share