473,569 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Removing elements from vector

I cannot relieve that I am working on Thanksgiving, but I am stuck over
something that should be simple. I have a vector that has twenty-four
elements. I need to remove the four elements whose positions equal
zero. The code below only removes two of the four. Any ideas?

logfile << "\n\n\n. . .removing flat positions";
logfile << "vector size=" << componentPerfor mance.size() << endl;
int position=0;
for (vector<cPerfor mance>::iterato r itr =
componentPerfor mance.begin();i tr!=componentPe rformance.end() ; ++itr)
{
position=itr->returnPosition (); //returnPosition( ) returns a float
logfile << endl << itr->returnSymbol () << "Position=" <<
itr->returnPosition ();
if(position==0)
{
logfile << "\tremoving . . . " << itr->returnSymbol() ;
componentPerfor mance.erase(itr );
}
}

logfile.close() ;

Nov 23 '06 #1
11 1976
Use typedefs!

if cPerformance is cheep to copy:

typedef ::std::vector<c Performance container_t;
container_t tmp;
::std::remove_c opy_if(componen tPerformance.be gin(),component Performance.end (),
::std::back_ins erter(tmp),
!::boost::bind( ::std::not_equa l_to<int>(),
::boost::bind(& cPerformance:: returnPosition, _1), 0));

componentPerfor mance.swap(tmp) ;

if it is not cheap to copy don't
keep them in a vector,

say container_t is a list
typedef ::std::list<cPe rformance container_t;

your loop would look something like:

for(container_t ::iterator it = componentPerfor mance.begin();
it != componentPerfor mance.end(); ++it)
{
if((*it). returnPosition( ) == 0)
it = componentPerfor mance.erase(it) ;
}

Nov 23 '06 #2

ka*****@hotmail .com wrote:
I cannot relieve that I am working on Thanksgiving, but I am stuck over
something that should be simple. I have a vector that has twenty-four
elements. I need to remove the four elements whose positions equal
zero. The code below only removes two of the four. Any ideas?

logfile << "\n\n\n. . .removing flat positions";
logfile << "vector size=" << componentPerfor mance.size() << endl;
int position=0;
for (vector<cPerfor mance>::iterato r itr =
componentPerfor mance.begin();i tr!=componentPe rformance.end() ; ++itr)
{
position=itr->returnPosition (); //returnPosition( ) returns a float
logfile << endl << itr->returnSymbol () << "Position=" <<
itr->returnPosition ();
if(position==0)
{
logfile << "\tremoving . . . " << itr->returnSymbol() ;
componentPerfor mance.erase(itr );
}
}

logfile.close() ;
Classic error :-)

When you "erase" the element, 'itr' in fact points already to next one
(technically, in fact it is invalidated by standard AFAIK). Therefore
++itr at the end of "for" skips the element next to the removed one.

Mirek

Nov 23 '06 #3

dasjotre wrote:
Use typedefs!

if cPerformance is cheep to copy:

typedef ::std::vector<c Performance container_t;
container_t tmp;
::std::remove_c opy_if(componen tPerformance.be gin(),component Performance.end (),
::std::back_ins erter(tmp),
!::boost::bind( ::std::not_equa l_to<int>(),
::boost::bind(& cPerformance:: returnPosition, _1), 0));

componentPerfor mance.swap(tmp) ;
No wonder nobody outside C++ ghetto considers C++ to be language
suitable to do any real work anymore :)
for(container_t ::iterator it = componentPerfor mance.begin();
it != componentPerfor mance.end(); ++it)
{
if((*it). returnPosition( ) == 0)
it = componentPerfor mance.erase(it) ;
}
I believe this code is wrong - it skips elements past removed one.

Nov 23 '06 #4

Mirek Fidler wrote:
dasjotre wrote:
Use typedefs!

if cPerformance is cheep to copy:

typedef ::std::vector<c Performance container_t;
container_t tmp;
::std::remove_c opy_if(componen tPerformance.be gin(),component Performance.end (),
::std::back_ins erter(tmp),
!::boost::bind( ::std::not_equa l_to<int>(),
::boost::bind(& cPerformance:: returnPosition, _1), 0));

componentPerfor mance.swap(tmp) ;

No wonder nobody outside C++ ghetto considers C++ to be language
suitable to do any real work anymore :)
A call for a simpler example !

inline bool operator == (cPerformance const & l, cPerformance const &
r)
{
return l.returnPositio n() == r.returnPositio n();
}

cPerformance nulperf(0);

::std::remove_c opy(componentPe rformance.begin (),componentPer formance.end(),
::std::back_ins erter(tmp), nulperf);
componentPerfor mance.swap(tmp) ;

Which one would teach you more?
for(container_t ::iterator it = componentPerfor mance.begin();
it != componentPerfor mance.end(); ++it)
{
if((*it). returnPosition( ) == 0)
{
it = componentPerfor mance.erase(it) ;
continue;
}
}

I believe this code is wrong - it skips elements past removed one.
Not any more ;)

Nov 23 '06 #5

dasjotre wrote:
Mirek Fidler wrote:
dasjotre wrote:
Use typedefs!
>
if cPerformance is cheep to copy:
>
typedef ::std::vector<c Performance container_t;
container_t tmp;
::std::remove_c opy_if(componen tPerformance.be gin(),component Performance.end (),
::std::back_ins erter(tmp),
!::boost::bind( ::std::not_equa l_to<int>(),
::boost::bind(& cPerformance:: returnPosition, _1), 0));
>
componentPerfor mance.swap(tmp) ;
No wonder nobody outside C++ ghetto considers C++ to be language
suitable to do any real work anymore :)

A call for a simpler example !

inline bool operator == (cPerformance const & l, cPerformance const &
r)
{
return l.returnPositio n() == r.returnPositio n();
}

cPerformance nulperf(0);

::std::remove_c opy(componentPe rformance.begin (),componentPer formance.end(),
::std::back_ins erter(tmp), nulperf);
componentPerfor mance.swap(tmp) ;

Which one would teach you more?
for(container_t ::iterator it = componentPerfor mance.begin();
it != componentPerfor mance.end(); ++it)
{
if((*it). returnPosition( ) == 0)
{
it = componentPerfor mance.erase(it) ;
continue;
}
}
I believe this code is wrong - it skips elements past removed one.

Not any more ;)
My humblest appologies.

container_t::it erator it = componentPerfor mance.begin();
while(it != componentPerfor mance.end())
{
if((*it).return Position() == 0)
it = componentPerfor mance.erase(it) ;
else
++it;
}

Good example to argue against hand coded loops

Nov 23 '06 #6
container_t::it erator it = componentPerfor mance.begin();
while(it != componentPerfor mance.end())
{
if((*it).return Position() == 0)
it = componentPerfor mance.erase(it) ;
else
++it;
}

Good example to argue against hand coded loops
Good example to teach you check twice before hitting Post button ;)

Do not worry, happens to me all the time.

Mirek

Nov 23 '06 #7

Mirek Fidler wrote:
container_t::it erator it = componentPerfor mance.begin();
while(it != componentPerfor mance.end())
{
if((*it).return Position() == 0)
it = componentPerfor mance.erase(it) ;
else
++it;
}

Good example to argue against hand coded loops

Good example to teach you check twice before hitting Post button ;)
I know, I'm a trigger happy poster :o)

Nov 23 '06 #8
ka*****@hotmail .com schrieb:
I cannot relieve that I am working on Thanksgiving, but I am stuck over
something that should be simple. I have a vector that has twenty-four
elements. I need to remove the four elements whose positions equal
zero. The code below only removes two of the four. Any ideas?
The componentPerfor mance.erase() update the iterator to the next
element. Hence, if you still want to use your for(...) loop, you have
to change the if statement as follows:

if(position==0)
{
logfile << "\tremoving . . . " << itr->returnSymbol() ;
componentPerfor mance.erase(itr );
--itr; // because itr points to the next element.
}

HTH,
Loic.

Nov 23 '06 #9

ka*****@hotmail .com wrote:
I cannot relieve that I am working on Thanksgiving, but I am stuck over
something that should be simple. I have a vector that has twenty-four
elements. I need to remove the four elements whose positions equal
zero. The code below only removes two of the four. Any ideas?

logfile << "\n\n\n. . .removing flat positions";
logfile << "vector size=" << componentPerfor mance.size() << endl;
int position=0;
for (vector<cPerfor mance>::iterato r itr =
componentPerfor mance.begin();i tr!=componentPe rformance.end() ; ++itr)
{
position=itr->returnPosition (); //returnPosition( ) returns a float
logfile << endl << itr->returnSymbol () << "Position=" <<
itr->returnPosition ();
if(position==0)
{
logfile << "\tremoving . . . " << itr->returnSymbol() ;
componentPerfor mance.erase(itr );
}
}

logfile.close() ;
template the functor...

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

struct VerifyPos0
{
bool operator()(cons t cPerformance& r_x)
{
// a float??
return 0 == static_cast<int >(r_x.returnPos ition());
}
};

int main()
{
std::vector< cPerformance componentPerfor mance;
...

componentPerfor mance.erase(
std::remove_if( componentPerfor mance.begin(),
componentPerfor mance.end(),
VerifyPos0()),
componentPerfor mance.end() );
...
}

Nov 23 '06 #10

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

Similar topics

1
2298
by: Tino | last post by:
I have a std::vector<int> which, after some initialization, has a fixed number of elements...after initialization I must do the following repeatedly: I remove an element which could be anywhere in the vector, and add another element which will always be at the end, ie. vector<int> v; int i, x; .... initialization
6
8003
by: Jason Heyes | last post by:
What is a good way of removing elements from std::vector so that the elements removed satisfy a predicate and end up stored in another std::vector. It seems as though the algorithm std::remove_if only achieves half the job. Here is how I would use std::remove_if to remove elements from std::vector based on predicate: ...
3
2714
by: Amit | last post by:
Hi, I have a list of integers. At each iteration, I remove some element from it and then insert new elements in it. The order of elements is not important. So I guess I could use a vector also for this purpose. However, I am also interested in having no duplicacy in elements of the vector. So I do not want to have any integer repeated more...
2
4723
by: vsgdp | last post by:
From what I learned, if you want to do random element insertions and deletions you should use a list. But, with std::vector, if the order of the elements does not matter, couldn't you efficiently remove a random element by swapping it with the last element and then just using pop_back? Does erase do this internally? Or does erase do the...
24
4358
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what we covered was within the last week of the class and all self taught. Our prof gave us an example of a Java method used to remove elements from an...
8
4879
by: Jason Heyes | last post by:
Does the STL have a function like this one? template <typename T> void remove(std::vector<T> &v, std::vector<T>::size_type index) { std::swap(v, v.back()); v.resize(index); } Unlike std::vector::erase, it calls T::operator= only three times no matter
2
1702
by: Adam Hartshorne | last post by:
Hi All, I was wondering if somebody could tell me if there is an efficient way to do the following. Say I have a list(or vector) A and a list B, I want to remove any elements in B, that are also elements in A. Adam
2
2134
by: forumsaregreat | last post by:
Hello All, I am currently trying to do the following: 1) I have a vector<string> which has 'n' number of elements (n is quite large order of 100,000) 2) Some of the elements in this vector are repeated Hence i need to get a resultant vector which will have only single elements (i.e. no repitition). What would a really fast scheme to achieve...
10
6052
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 remove the elements with odd values from your list * and the even values from your vector.
0
7695
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...
0
7922
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. ...
0
8119
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...
1
7668
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...
0
7964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5509
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...
0
3653
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...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.