473,406 Members | 2,217 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,406 software developers and data experts.

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=" << componentPerformance.size() << endl;
int position=0;
for (vector<cPerformance>::iterator itr =
componentPerformance.begin();itr!=componentPerform ance.end(); ++itr)
{
position=itr->returnPosition(); //returnPosition() returns a float
logfile << endl << itr->returnSymbol() << "Position=" <<
itr->returnPosition();
if(position==0)
{
logfile << "\tremoving. . . " << itr->returnSymbol();
componentPerformance.erase(itr);
}
}

logfile.close();

Nov 23 '06 #1
11 1966
Use typedefs!

if cPerformance is cheep to copy:

typedef ::std::vector<cPerformance container_t;
container_t tmp;
::std::remove_copy_if(componentPerformance.begin() ,componentPerformance.end(),
::std::back_inserter(tmp),
!::boost::bind(::std::not_equal_to<int>(),
::boost::bind(&cPerformance:: returnPosition, _1), 0));

componentPerformance.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<cPerformance container_t;

your loop would look something like:

for(container_t::iterator it = componentPerformance.begin();
it != componentPerformance.end(); ++it)
{
if((*it). returnPosition() == 0)
it = componentPerformance.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=" << componentPerformance.size() << endl;
int position=0;
for (vector<cPerformance>::iterator itr =
componentPerformance.begin();itr!=componentPerform ance.end(); ++itr)
{
position=itr->returnPosition(); //returnPosition() returns a float
logfile << endl << itr->returnSymbol() << "Position=" <<
itr->returnPosition();
if(position==0)
{
logfile << "\tremoving. . . " << itr->returnSymbol();
componentPerformance.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<cPerformance container_t;
container_t tmp;
::std::remove_copy_if(componentPerformance.begin() ,componentPerformance.end(),
::std::back_inserter(tmp),
!::boost::bind(::std::not_equal_to<int>(),
::boost::bind(&cPerformance:: returnPosition, _1), 0));

componentPerformance.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 = componentPerformance.begin();
it != componentPerformance.end(); ++it)
{
if((*it). returnPosition() == 0)
it = componentPerformance.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<cPerformance container_t;
container_t tmp;
::std::remove_copy_if(componentPerformance.begin() ,componentPerformance.end(),
::std::back_inserter(tmp),
!::boost::bind(::std::not_equal_to<int>(),
::boost::bind(&cPerformance:: returnPosition, _1), 0));

componentPerformance.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.returnPosition() == r.returnPosition();
}

cPerformance nulperf(0);

::std::remove_copy(componentPerformance.begin(),co mponentPerformance.end(),
::std::back_inserter(tmp), nulperf);
componentPerformance.swap(tmp);

Which one would teach you more?
for(container_t::iterator it = componentPerformance.begin();
it != componentPerformance.end(); ++it)
{
if((*it). returnPosition() == 0)
{
it = componentPerformance.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<cPerformance container_t;
container_t tmp;
::std::remove_copy_if(componentPerformance.begin() ,componentPerformance.end(),
::std::back_inserter(tmp),
!::boost::bind(::std::not_equal_to<int>(),
::boost::bind(&cPerformance:: returnPosition, _1), 0));
>
componentPerformance.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.returnPosition() == r.returnPosition();
}

cPerformance nulperf(0);

::std::remove_copy(componentPerformance.begin(),co mponentPerformance.end(),
::std::back_inserter(tmp), nulperf);
componentPerformance.swap(tmp);

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

Not any more ;)
My humblest appologies.

container_t::iterator it = componentPerformance.begin();
while(it != componentPerformance.end())
{
if((*it).returnPosition() == 0)
it = componentPerformance.erase(it);
else
++it;
}

Good example to argue against hand coded loops

Nov 23 '06 #6
container_t::iterator it = componentPerformance.begin();
while(it != componentPerformance.end())
{
if((*it).returnPosition() == 0)
it = componentPerformance.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::iterator it = componentPerformance.begin();
while(it != componentPerformance.end())
{
if((*it).returnPosition() == 0)
it = componentPerformance.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 componentPerformance.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();
componentPerformance.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=" << componentPerformance.size() << endl;
int position=0;
for (vector<cPerformance>::iterator itr =
componentPerformance.begin();itr!=componentPerform ance.end(); ++itr)
{
position=itr->returnPosition(); //returnPosition() returns a float
logfile << endl << itr->returnSymbol() << "Position=" <<
itr->returnPosition();
if(position==0)
{
logfile << "\tremoving. . . " << itr->returnSymbol();
componentPerformance.erase(itr);
}
}

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

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

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

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

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

Nov 23 '06 #10
if(position==0)
{
logfile << "\tremoving. . . " << itr->returnSymbol();
componentPerformance.erase(itr);
--itr; // because itr points to the next element.
}
AFAIK Undefined behavior if the first element is removed (--itr will
move before the .begin()).

Mirek

Nov 23 '06 #11
In article <11**********************@k70g2000cwa.googlegroups .com>,
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=" << componentPerformance.size() << endl;
int position=0;
for (vector<cPerformance>::iterator itr =
componentPerformance.begin();itr!=componentPerform ance.end(); ++itr)
{
position=itr->returnPosition(); //returnPosition() returns a float
logfile << endl << itr->returnSymbol() << "Position=" <<
itr->returnPosition();
if(position==0)
{
logfile << "\tremoving. . . " << itr->returnSymbol();
componentPerformance.erase(itr);
}
}

logfile.close();
componentPerformance.erase( remove_if( componentPerformance.begin(),
componentPerformance.end(),
not1( mem_fun_ref( &cPerformance::returnPosition ) ) ),
componentPerformance.end() );
Or if you have/want to write it out:

vector<cPerformance>::iterator begin = componentPerformance.begin();
vector<cPerformance>::iterator end = componentPerformance.end();
vector<cPerformance>::iterator pos = componentPerformance.begin();
while ( begin != end ) {
if ( begin->returnSymbol() == 0.0 )
++begin;
else
*pos++ = *begin++;
}
componentPerformance.erase( pos, end );

--
To send me email, put "sheltie" in the subject.
Nov 24 '06 #12

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

Similar topics

1
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...
6
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...
3
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...
2
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...
24
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...
8
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...
2
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...
2
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...
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...
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: 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
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
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...
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...
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,...
0
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...

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.