473,395 Members | 1,386 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,395 software developers and data experts.

STL vector and erase : HELP

Hi,

I have a vector of type TReferenceItem(structure) and I would like to erase
one of the TReferenceItem inside the vector based on 2 criteria of the
TReferenceItem

vector<TReferenceItem>::iterator itVec;

for ( i = 0, itVec = m_RefItemVec.begin() ; itVec != m_RefItemVec.end();
itVec++, i++)
{
nCompIDServer = memcmp(m_RefItemVec[i].IDServer, AIDServer,
sizeof(TARSIDServer));
nCompIDUser = (m_RefItemVec[i].IDUser == AIDUser)?0:1;

if (nCompIDServer == 0 && nCompIDUser == 0) // If the 2 criteria match
{
m_RefItemVec.erase(itVec); // Delete the reference
}

}

Actually my problem is simple I whant to get through each element of my
vector and if the element match my conditions I want to remove it.

I tried the above code but it doesn't work because if the TReferenceItem to
remove is the last element when i do : m_RefItemVec.erase(itVec) the
iterator.end is modifief so that my stop condition is never realized and i
have a forever loop.
What is the solution ?
Jul 19 '05 #1
2 7337
I tried also this and it is not better :

for (itVec = m_RefItemVec.begin() ; itVec != m_RefItemVec.end(); ++itVec)
{
AfxMessageBox( _T("LOOP") );
nCompIDServer = memcmp((*itVec).IDServer, AIDServer,
sizeof(TARSIDServer));
nCompIDUser = ((*itVec).IDUser == AIDUser)?0:1;
if (nCompIDServer == 0 && nCompIDUser == 0) // If the 2 criteria match
{
AfxMessageBox( _T("I have deleted") );
m_RefItemVec.erase(itVec); // Delete the reference
}

}

"mosfet" <tr******@wanadoo.fr> a écrit dans le message de
news:bp**********@news-reader3.wanadoo.fr...
Hi,

I have a vector of type TReferenceItem(structure) and I would like to erase one of the TReferenceItem inside the vector based on 2 criteria of the
TReferenceItem

vector<TReferenceItem>::iterator itVec;

for ( i = 0, itVec = m_RefItemVec.begin() ; itVec != m_RefItemVec.end();
itVec++, i++)
{
nCompIDServer = memcmp(m_RefItemVec[i].IDServer, AIDServer,
sizeof(TARSIDServer));
nCompIDUser = (m_RefItemVec[i].IDUser == AIDUser)?0:1;

if (nCompIDServer == 0 && nCompIDUser == 0) // If the 2 criteria match
{
m_RefItemVec.erase(itVec); // Delete the reference
}

}

Actually my problem is simple I whant to get through each element of my
vector and if the element match my conditions I want to remove it.

I tried the above code but it doesn't work because if the TReferenceItem to remove is the last element when i do : m_RefItemVec.erase(itVec) the
iterator.end is modifief so that my stop condition is never realized and i have a forever loop.
What is the solution ?

Jul 19 '05 #2
"mosfet" <tr******@wanadoo.fr> wrote in message
news:bp**********@news-reader3.wanadoo.fr...
| I have a vector of type TReferenceItem(structure) and I would like to
erase
| one of the TReferenceItem inside the vector based on 2 criteria of the
| TReferenceItem
|
| vector<TReferenceItem>::iterator itVec;
|
| for ( i = 0, itVec = m_RefItemVec.begin() ; itVec != m_RefItemVec.end();
| itVec++, i++)
| {
[...] if( ... some condition ... )
| {
| m_RefItemVec.erase(itVec); // Delete the reference
| }
| }

This loop not only leads to UB if the last item is removed,
but it will fail to remove the second of two consecutive
items to be removed.

Also, the approach you are using to test the condition seems
incorrect:
| nCompIDServer = memcmp(m_RefItemVec[i].IDServer, AIDServer,
| sizeof(TARSIDServer));
| nCompIDUser = (m_RefItemVec[i].IDUser == AIDUser)?0:1;
You should use (*itVec) instead of m_RefItemVec[i],
because once you remove an entry the index of all the
following items will be shifted...

| What is the solution ?

When an item is removed, the iterator shall not be incremented.
So the correct code is:
for( it = vec.begin() ; it != vec.end() ; /*empty*/ )
if( needToRemove(*it) )
vec.erase( it );
else
++it;
Also, you should consider using the standard remove_if
from header <algorithm>:
vec.erase( remove_if( vec.begin(), vec.end(), RemoveTest() )
, vec.end() );
Where RemoveTest needs to be a predicate (a unary function object).
It would be a good idea to learn about this.
See for example:
http://www.sgi.com/tech/stl/functors.html
or other references discussed in this NG...
I hope this helps,
Ivan
--
http://ivan.vecerina.com
Jul 19 '05 #3

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

Similar topics

0
by: marco_segurini | last post by:
Hi, I am trying to solve this problem: I have a vector V1 that is not empty and a vector V2 that contains some iterator that point to V1 elements. Now I want to remove the V1 elements that V2...
3
by: Jonathan | last post by:
Hey again everyone! I have another question for you guys. I am trying to erase a certain vector element based on what number the user selects. Here is what I did: case 'b' : { cout << "Please...
9
by: david wolf | last post by:
I want to delete all even numbers in a vector, I am not sure if there's any better way to do it. Following program is how I did it. Look at the part of code beginning from comments: //delete all...
5
by: Billy Patton | last post by:
I have a polygon loaded into a vector. I need to remove redundant points. Here is an example line segment that shows redundant points a---------b--------c--------d Both b and c are not...
14
by: cayblood | last post by:
I want to iterate through a vector and erase elements that meet a certain criteria. I know there is an algorithmic way of doing this, but first I would like to know how to do it with normal...
9
by: Amadeus W. M. | last post by:
I have a vector from which I want to erase elements between iterators i,j. If i<j, everything works as expected, but if j>i, an insertion is actually performed. Example: vector<double> x(10);...
3
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
I have some code where there's this vector of pointers to objects and I need to delete and erase some of them, the problem is that to know which I need to iterate through the vector and I'm trying...
10
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to...
5
by: Anil | last post by:
I am facing problem while erasing an elemet from stl vector when its size is 2. It works fine when SIZE 2. Can anybody help me in this?? Following is the sample code which i tried. #include...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.