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

stl <algorithm>: How to delete selectively from vector?

Hello,

I have got a list of indices stored as a stl::vector and a range given by
two iterators, lets say [from, to]. The values in this range are not
ordered, but I have another range of values, lets say lower to upper. Now I
want to remove all values from the stl vector in range [from, to] having
values in [lower, upper]. I have just tried around with remove_if and some
predicates, but I'm not used to the algorithms library. Perhaps someone can
help me.
std::vector<long> indices; // e.g. [1 3 6 2 1 4 6 2 4 1 3 5 7]
^ ^
std::vector<long>::iterator from, to; // e.g. from = indices.begin() + 3,
// to = indices.begin() + 8;
long lower, upper // e.g. lower = 1, upper = 3;
What would it look like?

std::remove_if(from, to, ???????);

regards,
Alex

Jul 22 '05 #1
5 2458
Alexander Stippler wrote:
I have got a list of indices stored as a stl::vector and a range given by
two iterators, lets say [from, to]. The values in this range are not
ordered, but I have another range of values, lets say lower to upper. Now I
want to remove all values from the stl vector in range [from, to] having
values in [lower, upper]. I have just tried around with remove_if and some
predicates, but I'm not used to the algorithms library. Perhaps someone can
help me.
Alex, why don't you _get_used_ to the algorithms? Do you have a copy
of Josuttis' "The C++ Standard Library"? If yes, use it. If not, get
it. Today.
std::vector<long> indices; // e.g. [1 3 6 2 1 4 6 2 4 1 3 5 7]
^ ^
std::vector<long>::iterator from, to; // e.g. from = indices.begin() + 3,
// to = indices.begin() + 8;
long lower, upper // e.g. lower = 1, upper = 3;
What would it look like?

std::remove_if(from, to, ???????);


struct MyRemovalPredicate {
long lower, upper;
MyRemovalPredicate(long l, long u) : lower(l), upper(u) {}
bool operator()(long number) const {
return (number >= lower) && (number <= upper);
}
};

....
std::vector<long>::iterator last = std::remove_if(from, to,
MyRemovalPredicate(lower, upper));
indices.erase(to, last); // actually removes them

[Disclaimer: this is all untested, from memory, for illustration only]

V
Jul 22 '05 #2
Alexander Stippler wrote:
Hello,

I have got a list of indices stored as a stl::vector and a range
given by two iterators, lets say [from, to]. The values in this range
are not ordered, but I have another range of values, lets say lower
to upper. Now I want to remove all values from the stl vector in
range [from, to] having values in [lower, upper]. I have just tried
around with remove_if and some predicates, but I'm not used to the
algorithms library. Perhaps someone can help me.
std::vector<long> indices; // e.g. [1 3 6 2 1 4 6 2 4 1 3 5 7]
^ ^
std::vector<long>::iterator from, to; // e.g. from =
indices.begin() + 3, //
to = indices.begin() + 8;
long lower, upper // e.g. lower = 1, upper = 3;
What would it look like?

std::remove_if(from, to, ???????);


You need a function or functor to implement your custom predicate:

bool is_in_range(int value)
{
return (value >= lower && value <= upper);
}

then call it as follows:
std::remove_if(from, to, is_in_range);

The problem with that is that lower and upper need to be global. If you want
to set them based on local variables not known at compile time, you need to
use a functor:
class is_in_range
{
public:
is_in_range(long lower, long upper) : _lower(lower), _upper(upper) { }
bool operator() (long value)
{
return (value >= _lower && value <= _upper);
}
private:
long _lower;
long _upper;
};

Then call it as follows:
std::remove_if(from, to, is_in_range(lower, upper));

--
Unforgiven

Jul 22 '05 #3
Unforgiven wrote:
<snip>
Then call it as follows:
std::remove_if(from, to, is_in_range(lower, upper));


That does not actually remove the items, you need this instead:
yourvector.erase(std::remove_if(yourvector.begin() , yourvector.end(),
is_in_range(-10, +10), yourvector.end());

- Pete
Jul 22 '05 #4
Petec wrote:
Unforgiven wrote:
<snip>
Then call it as follows:
std::remove_if(from, to, is_in_range(lower, upper));

That does not actually remove the items, you need this instead:
yourvector.erase(std::remove_if(yourvector.begin() , yourvector.end(),
is_in_range(-10, +10), yourvector.end());


This should probably be

yourvector.erase(
std::remove_if(from, to, is_in_range(lower,upper)), to);

Victor
Jul 22 '05 #5
Victor Bazarov wrote:
Alex, why don't you _get_used_ to the algorithms? Do you have a copy
of Josuttis' "The C++ Standard Library"? If yes, use it. If not, get
it. Today.


First, thanks for your hints. I'm quite interested in the algorithms. But
at the moment I just haven't got the time for an in-depth-study, so I'm
grateful for your help so far. And having a look at the algorithms I already
noticed that they could be helpful in much more places than I thought so
far. So I will definitely have a closer look some (hopefully near) day.

Best regards,
Alex
Jul 22 '05 #6

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

Similar topics

20
by: Steffen Brinkmann | last post by:
Hi! I tried to modify the transform algorithm in a way that it doesn't take iterators, but a reference to a container class and a value, because Mostly I need to do an operation of a container...
5
by: google | last post by:
Hi All, I'm just getting started learning to use <algorithm> instead of loads of little for loops, and I'm looking for a bit of advice/mentoring re: implementing the following... I have a...
7
by: Wei | last post by:
Hi all, I found out I can use the max function which is defined in STL <algorithmwithout including this header in my program. The compilers I used are GNU g++ 3.4.4 and Visual Studio C++ 2005. ...
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...
11
by: Gerald I. Evenden | last post by:
Working on a Kubuntu 64bit system "c++ (GCC) 4.0.3". The following simple program extracted from p.497 & 499 of N.M.Josurris' "The C++ Standard Library ... " (file t.cpp): 1 #include <string>...
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...
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
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
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
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
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.