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

erase-remove usage

Hello,

I have the following code:

////////////////////////////

class Obj
{
public:
int time;
string str;
};

vector<Objarray;

////////////////////////

I would like to remove all the "Obj" objects where their "time"
variables are less than or equal to, say x. Could anyone teach me how
to use erase-remove to do that?

Thank you so much!

Nov 4 '06 #1
3 1666
Wing wrote:
Hello,

I have the following code:

////////////////////////////

class Obj
{
public:
int time;
string str;
};

vector<Objarray;

////////////////////////

I would like to remove all the "Obj" objects where their "time"
variables are less than or equal to, say x. Could anyone teach me how
to use erase-remove to do that?
Read up on std::vector.remove_if(). a google for "vector remove_if"
will find you some examples.

--
Ian Collins.
Nov 4 '06 #2
Hello,

I have googled for this topic. However, all the examples I saw are
based on vector<int>.

I think that I need to add some extra functions into my code in order
to do what I want. Anyone can give me some pointer? I am very
confused.... :(

Thank you.
Ian Collins wrote:
Wing wrote:
Hello,

I have the following code:

////////////////////////////

class Obj
{
public:
int time;
string str;
};

vector<Objarray;

////////////////////////

I would like to remove all the "Obj" objects where their "time"
variables are less than or equal to, say x. Could anyone teach me how
to use erase-remove to do that?
Read up on std::vector.remove_if(). a google for "vector remove_if"
will find you some examples.

--
Ian Collins.
Nov 4 '06 #3
Wing wrote:

Please don't top post.
Ian Collins wrote:
>>Wing wrote:
>>>Hello,

I have the following code:

////////////////////////////

class Obj
{
public:
int time;
string str;
};

vector<Objarray;

////////////////////////

I would like to remove all the "Obj" objects where their "time"
variables are less than or equal to, say x. Could anyone teach me how
to use erase-remove to do that?

Read up on std::vector.remove_if(). a google for "vector remove_if"
will find you some examples.
Hello,

I have googled for this topic. However, all the examples I saw are
based on vector<int>.
Probably because it's the most straightforward case!
I think that I need to add some extra functions into my code in order
to do what I want. Anyone can give me some pointer? I am very
confused.... :(
Extending your Obj to give an example:

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

struct Obj
{
int time;
std::string str;
Obj( int time ) : time(time) {}
friend std::ostream& operator<<( std::ostream& out, const Obj& o ){
return out << o.time;
}
};

struct RemoveAfter : std::unary_function<Obj, bool>
{
bool operator()(const Obj& x){ return x.time 10; }
};

int main() {
std::vector<Objarray;

array.push_back( Obj( 5 ) );
array.push_back( Obj( 15 ) );
array.push_back( Obj( 10 ) );

std::copy(array.begin(),
array.end(),
std::ostream_iterator<Obj>(std::cout," "));
std::cout << std::endl;

std::vector<Obj>::iterator last = std::remove_if( array.begin(),
array.end(),
RemoveAfter() );
std::copy(array.begin(),
last,
std::ostream_iterator<Obj>(std::cout," "));
std::cout << std::endl;
}

--
Ian Collins.
Nov 4 '06 #4

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

Similar topics

9
by: Harald Grossauer | last post by:
Usually STL containers have something like "iterator container<>::erase(iterator)" which erases the element the iterator points to and returns another valid iterator. Set does not have this,...
5
by: Angus Leeming | last post by:
Dinkumware's online STL reference http://tinyurl.com/3es52 declares std::map's overloaded erase member functions to have the interface: map::erase iterator erase(iterator where); iterator...
26
by: Pieter Thysebaert | last post by:
Hello, I've got a question conerning erasing key-value pairs from a std::map while iterating over it. According to the STL docs, erasing an element in a map invalidates all iterators pointing...
3
by: jose luis fernandez diaz | last post by:
Hi, Erase elements while iterating on a map don't invalidate the iterator except the erased one, so the program below: (1) #include <map> int main()
20
by: Tom van Stiphout | last post by:
I'm about to write a function like below, which I'm going to call a lot of times. So I care about possible memory leaks. I think whether I should use Erase or not depends on whether Split creates...
8
by: olanglois | last post by:
Hi, I was asking myself to following question. What is better to erase an element from a STL map: calling (option #1) size_type erase(const key_type& k) or calling (option #2)
11
by: moleskyca1 | last post by:
Hi, I know if you call erase when you iterate through map you will crash. Ex: map<int,doublem; // insert something for ( map<int, double>::iterator i = m.begin(); i != m.end(); i++ ) if (...
6
by: alon | last post by:
I got the following situation: I have a few lists of integers, and I have an iterator pointing to one of the elements. I don't have a pointer to the list itself ! All I want is to remove the...
4
by: sks | last post by:
I have a question regarding std::multimap/iterators. At the SGI website, it says "Erasing an element from a multimap also does not invalidate any iterators, except, of course, for iterators that...
3
by: subramanian100in | last post by:
Consider vector<stringv; If we call, v.erase(v.end()) this invokes undefined behaviour. But, if we call
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.