473,786 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<lon g> indices; // e.g. [1 3 6 2 1 4 6 2 4 1 3 5 7]
^ ^
std::vector<lon g>::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 2476
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<lon g> indices; // e.g. [1 3 6 2 1 4 6 2 4 1 3 5 7]
^ ^
std::vector<lon g>::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 MyRemovalPredic ate {
long lower, upper;
MyRemovalPredic ate(long l, long u) : lower(l), upper(u) {}
bool operator()(long number) const {
return (number >= lower) && (number <= upper);
}
};

....
std::vector<lon g>::iterator last = std::remove_if( from, to,
MyRemovalPredic ate(lower, upper));
indices.erase(t o, 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<lon g> indices; // e.g. [1 3 6 2 1 4 6 2 4 1 3 5 7]
^ ^
std::vector<lon g>::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(lon g 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(low er, upper));

--
Unforgiven

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


That does not actually remove the items, you need this instead:
yourvector.eras e(std::remove_i f(yourvector.be gin(), 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_i f(from, to, is_in_range(low er, upper));

That does not actually remove the items, you need this instead:
yourvector.eras e(std::remove_i f(yourvector.be gin(), yourvector.end( ),
is_in_range(-10, +10), yourvector.end( ));


This should probably be

yourvector.eras e(
std::remove_if( from, to, is_in_range(low er,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
3854
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 and a single number (e.g. multiply all the values in a vector by 3 or so). So, this is my intent: #ifndef ALGORITHM_EXT_HH #define ALGORITHM_EXT_HH
5
4323
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 vector of boost::shared_ptr's, some of which may be NULL. I'd like to find out if there are any non-NULL elements in the vector. Here's what I have at the moment.
7
2608
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. The program is as follows: #include <iostream> //#include <algorithm <-- I don't have to include <algorithmin
10
6077
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 remove the elements with odd values from your list * and the even values from your vector.
11
2992
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> 2 #include <iostream> 3 #include <algorithm> 4 #include <cctype> 5 using namespace std;
0
9650
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7515
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.