473,406 Members | 2,620 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.

c++ and vector

Hello everyone.

I have a question about using vector.

Given a vector<intwith 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.

Sep 20 '06 #1
3 1307
Wing wrote:
Hello everyone.

I have a question about using vector.

Given a vector<intwith 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<intV;
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
Sep 20 '06 #2
Stuart Redmann wrote:
Wing wrote:
>Hello everyone.

I have a question about using vector.

Given a vector<intwith 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<intV;
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:
// decreased.
This should be replaced by
// not 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
Sep 20 '06 #3
Wing wrote:
Hello everyone.

I have a question about using vector.

Given a vector<intwith 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
>
Thanks.
Sep 20 '06 #4

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

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
16
by: Martin Jørgensen | last post by:
Hi, I get this using g++: main.cpp:9: error: new types may not be defined in a return type main.cpp:9: note: (perhaps a semicolon is missing after the definition of 'vector') main.cpp:9:...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
6
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
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
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
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,...
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
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
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.