473,785 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>::i terator 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 9978
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>::i terator 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>::i terator itr = srcVector; itr != srcVector; itr++) {
int i = (*itr);

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

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

vector<int>::it erator posDest
= std::find (destVector.beg in(), destVector.end( ), *itr)

if (posDest != destVector.end( )) {
srcVector.remov e (itr);
destVector.remo ve (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.c om (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>::eras e(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>::iter ator, 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.c om (Roland Pibinger)
wrote:
if (posDest != destVector.end( )) {
srcVector.remov e (itr);
destVector.remo ve (posDest);
} else {
++itr;
}
}


The correct code is:

if (posDest != destVector.end( )) {
itr = srcVector.erase (itr);
destVector.eras e (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
2633
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 http://aspn.activestate.com/ASPN/Mail/Message/python-tutor/2302348
0
1100
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 use push_back on all items except item i want to delete or is there any smoother way to do that ? would this do the job ? template <typename T> void TVecArray<T>::remove (iterator i)
9
3306
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
2747
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
2711
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 needed for container modifications, item cannot know its iterator, at least its not easy to do so and i think cannot be done type-safe avoiding virtual functions and stuff. 'this' pointer from items is a freeby :>. the problem is, std...
6
2966
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 = room->living.begin(); while (iter != room->living.end()) {
37
19208
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
5539
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. ----------------------------------------------------------------------- List<stringarstr = new List<string>(); //init data for (int i = 0; i < 30; ++i)
2
9095
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 = "Test" & x controls.add(btn) next x This results in 10 buttons appearing on the screen. I've excluded the
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9485
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10161
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.