How can I remove an item in STL iterator?

A

Allerdyce.John

How can I emove an item in STL iterator without use the STL algorithm?
I know we can use stl erase, find_if to remove items from a STL vector
, but how can I do the same without using STL algorithm?

vector<int> srcVector;
vector<int> destVector;

for (vector<int>::iterator itr = srcVector; itr != srcVector; itr++) {
int i = (*itr);

// if i contains in destVector, remove that from both srcVector
and destVector

}
 
T

TB

(e-mail address removed) skrev:
How can I emove an item in STL iterator without use the STL algorithm?
I know we can use stl erase, find_if to remove items from a STL vector
, but how can I do the same without using STL algorithm?

vector<int> srcVector;
vector<int> destVector;

for (vector<int>::iterator itr = srcVector; itr != srcVector; itr++) {
int i = (*itr);

// if i contains in destVector, remove that from both srcVector
and destVector

}

std::vector<int> v(10);
v.erase(v.begin() + 4); // deletes 5th element
 
R

Roland Pibinger

How can I emove an item in STL iterator without use the STL algorithm?
I know we can use stl erase, find_if to remove items from a STL vector
, but how can I do the same without using STL algorithm?

vector<int> srcVector;
vector<int> destVector;

for (vector<int>::iterator itr = srcVector; itr != srcVector; itr++) {
int i = (*itr);

// if i contains in destVector, remove that from both srcVector
and destVector


for (vector<int>::iterator itr = srcVector.begin();
itr != srcVector.end(); ) {

vector<int>::iterator posDest
= std::find (destVector.begin(), destVector.end(), *itr)

if (posDest != destVector.end()) {
srcVector.remove (itr);
destVector.remove (posDest);
} else {
++itr;
}
}

Not tested. Does not work for duplicate entries in srcVector or
destVector. Replace 'std::find' with your own 'find' function if you
don't want std::algorithms.

Best wishes,
Roland Pibinger
 
A

Allerdyce.John

Thanks. My question is: "Is it save to call remove of a STL container
as you are iterator thru the same STL container ? " Like the example
above posting.
 
R

Roland Pibinger

Thanks. My question is: "Is it save to call remove of a STL container
as you are iterator thru the same STL container ? " Like the example
above posting.

Actually the right remove function is erase, e.g,
srcVector.erase (itr);

WRT to your question: "iterators and references become invalid only
from the first element erased through the end of the sequence"
http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=vector.html#vector::erase

Best wishes,
Roland Pibigner
 
A

Acer

codes below are SGI STL sourcecode for vector<T>::erase(iterator
position);

they are inline in .h

iterator erase(iterator position)
{
if( position+1 != end() )
copy( position+1, finish, position);
//u can find this function's definition in <algorithm>
--finish;
destory(finish); //<memory>; part of the allocator
return position;
}

Because finish's type is vector<T>::iterator, and actually is T*.
Therefore, destory will call finish->~T(); to deconstruct item.
 
R

Roland Pibinger

if (posDest != destVector.end()) {
srcVector.remove (itr);
destVector.remove (posDest);
} else {
++itr;
}
}

The correct code is:

if (posDest != destVector.end()) {
itr = srcVector.erase (itr);
destVector.erase (posDest);
} else {
++itr;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top