C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

c++ and vector

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Wing
Guest





PostPosted: Wed Sep 20, 2006 9:10 am    Post subject: c++ and vector Reply with quote



Hello everyone.

I have a question about using vector.

Given a vector<int> with a lot of integers stored inside, I would like
to delete any elements bigger than some value, say x. Is it any
efficient way to do that?

Thanks.
Back to top
Stuart Redmann
Guest





PostPosted: Wed Sep 20, 2006 9:10 am    Post subject: Re: c++ and vector Reply with quote



Wing wrote:

Quote:
Hello everyone.

I have a question about using vector.

Given a vector<int> with a lot of integers stored inside, I would like
to delete any elements bigger than some value, say x. Is it any
efficient way to do that?

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

using namespace std;

int main ()
{
vector<int> V;
V.push_back (2);
V.push_back (7);
V.push_back (5);
V.push_back (3);
V.push_back (9);
V.push_back (1);

// Print the vector.
copy (V.begin(), V.end(), ostream_iterator<int>(cout, " "));
cout << endl;

// Invoke remove_if: This method will move all elements that make
// the predicate bind2nd (greater<int>(), 5) true to the front of
// the vector. The return value of this invocation is the first
// element that doesn't belong to the controlled sequence (the
// vector's size is unchanged after the call). These elements are
// deleted by the next call to erase (). Although this looks a bit
// confusing, this feature can be rather handy: This way unneeded
// re-allocations are avoided since the vector's size has
// decreased.
vector<int>::iterator new_end =
remove_if(V.begin(), V.end(),
bind2nd (greater<int>(), 5));

V.erase(new_end, V.end());

// Print the vector again.
copy(V.begin(), V.end(), ostream_iterator<int>(cout, " "));

return 0;
}

Regards,
Stuart
Back to top
Stuart Redmann
Guest





PostPosted: Wed Sep 20, 2006 9:10 am    Post subject: Re: c++ and vector Reply with quote



Stuart Redmann wrote:

Quote:
Wing wrote:

Hello everyone.

I have a question about using vector.

Given a vector<int> with a lot of integers stored inside, I would like
to delete any elements bigger than some value, say x. Is it any
efficient way to do that?


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

using namespace std;

int main ()
{
vector<int> V;
V.push_back (2);
V.push_back (7);
V.push_back (5);
V.push_back (3);
V.push_back (9);
V.push_back (1);

// Print the vector.
copy (V.begin(), V.end(), ostream_iterator<int>(cout, " "));
cout << endl;

// Invoke remove_if: This method will move all elements that make
// the predicate bind2nd (greater<int>(), 5) true to the front of
// the vector. The return value of this invocation is the first
// element that doesn't belong to the controlled sequence (the
// vector's size is unchanged after the call). These elements are
// deleted by the next call to erase (). Although this looks a bit
// confusing, this feature can be rather handy: This way unneeded
// re-allocations are avoided since the vector's size has

Sorry for the typo:
Quote:
// decreased.
This should be replaced by

// not decreased.

Quote:
vector<int>::iterator new_end =
remove_if(V.begin(), V.end(),
bind2nd (greater<int>(), 5));

V.erase(new_end, V.end());

// Print the vector again.
copy(V.begin(), V.end(), ostream_iterator<int>(cout, " "));

return 0;
}

Regards,
Stuart
Back to top
Carlos Martinez
Guest





PostPosted: Wed Sep 20, 2006 9:10 am    Post subject: Re: c++ and vector Reply with quote

Wing wrote:
Quote:
Hello everyone.

I have a question about using vector.

Given a vector<int> with a lot of integers stored inside, I would like
to delete any elements bigger than some value, say x. Is it any
efficient way to do that?

try remove_if

Quote:

Thanks.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.