Vector question

P

Pat

Give a vector of int, I want to remove all integers larger than L. What
method should be used?

Thanks. Pat
 
J

John Harrison

Pat said:
Give a vector of int, I want to remove all integers larger than L. What
method should be used?

Thanks. Pat

It the sort of operation the STL is tailor made for

#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
using namespace std;

int main()
{
int data[10] = { 1, 2, 6, 2, 4, 6, 3, 1, 4, 5 };
vector<int> v(data, data + 10);

// remove all elements greater than 3
v.erase(remove_if(v.begin(), v.end(), bind2nd(greater<int>(), 3)),
v.end());

copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
cout << '\n';
}

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top