473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How can I remove an item in STL iterator?

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

}

Mar 5 '06 #1
7 9958
TB
Al************@gmail.com 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

--
TB @ SWEDEN
Mar 5 '06 #2
On 5 Mar 2006 09:20:48 -0800, in comp.lang.c++ you wrote:
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
Mar 5 '06 #3
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.

Mar 5 '06 #4
On 5 Mar 2006 11:45:16 -0800, Al************@gmail.com wrote:
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/re...#vector::erase

Best wishes,
Roland Pibigner

Mar 5 '06 #5
On Sun, 05 Mar 2006 20:56:32 GMT, rp*****@yahoo.com (Roland Pibinger)
wrote:
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/re...#vector::erase


After having read that sentence again I see that my solution is
probably not valid.

Sorry for the confusion. STL is too complicated for a quick answer.
Roland Pibinger

Mar 5 '06 #6
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.

Mar 6 '06 #7
On Sun, 05 Mar 2006 18:33:10 GMT, rp*****@yahoo.com (Roland Pibinger)
wrote:
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;
}
}

Mar 6 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Ville Vainio | last post by:
For quick-and-dirty stuff, it's often convenient to flatten a sequence (which perl does, surprise surprise, by default): ]]] -> One such implementation is at ...
0
by: ScOe | last post by:
I've made a container class over std::allocator. Also implement create, clear, push_back but i need some assistance on removing item from container. do i need to create empty container and than...
9
by: Gunnar G | last post by:
Hello, I just want to make sure I got it right. I have this code list<int> L; L.push_back(1); L.push_back(2); L.push_back(3); L.push_back(4); list<int>::iterator iter=L.begin()+1;
11
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
16
by: forester | last post by:
lets say its common situation when object have subobjects in container and receives callbacks from contained items. and object want to move objects in containers on signal(callback). iterator is...
6
by: Jonathan | last post by:
Hi. I'm having trouble figuring out what I should be doing here. I'm trying to remove an object from a list. The function is: void Alive::FromRoom () { list<Alive>::iterator iter =...
37
by: bahoo | last post by:
Hi, I have a list like and as output I want If I myList.remove('0024') then only the first instance of '0024' is removed.
4
by: =?Utf-8?B?emhhbmdscg==?= | last post by:
Hi, Following code does not work. (it will crash.)-- I think the reason is that I cannot modify list (call remove) in foreach. But I don't know what I can do. ...
2
by: =?Utf-8?B?R3JlZw==?= | last post by:
I have the following code the dynamically adds a specific number of controls. for x as integer = 1 to 10 Dim btn as Windows.Forms.Button = New Windows.Forms.Button btn.Name = "btn" & x btn.Text...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.