473,805 Members | 2,297 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

standard library algorithms

Are the standard library functions pertinent to both sequence containers and
associative containers?

For example, if "find_if", "remove_if" , etc... valid for both lists, deques,
vectors, sets, and maps?

I know that the word "iterator" is typedef'd from something for everyone of
these containers so I was curious if all these functions are valid.

On the same note, should I use "find_if" or "remove_if" for the following
template:

template <class AssociativeCont ainer, class Predicate>
inline void remove_if(Assoc iativeContainer & C, Predicate pred,
class associative_con tainer_tag)
{
typedef typename AssociativeCont ainer::iterator iterator;
iterator cur = c.begin();
const iterator last = c.end();

while ( (cur = std::find_if(cu r, last, pred)) != last)
{
iterator tmp = cur++;
c.erase(tmp);
}
}
Jul 19 '05 #1
5 3038
sks_cpp wrote:
Are the standard library functions pertinent to both sequence containers and
associative containers?

For example, if "find_if", "remove_if" , etc... valid for both lists, deques,
vectors, sets, and maps?
These two functions take two objects of some forward iterator type as
arguments (the first forward iterator must be mutable). A container
which provides forward iterators is called a forward container.
Therefore, find_if can be used with any forward container. The same
applies to remove_if but the forward iterator type has to be a mutable
iterator type as well, so you have to have a mutable container.

You get a mutable container just by declaring it non-const. Mutable
iterators (e.g. vector <int>::iterator ) come from mutable containers.
A container that is declared const is not a mutable container, and the
iterators that come from them are not mutable. For example, `vector
<int>::const_it erator' is different from `const vector <int>::iterator '.

All five of the containers you mention are forward containers, so yes,
you can use both functions with all of the containers. In addition, a
pointer is a random access iterator and so certainly a forward iterator,
so you can use find_if with an array by passing pointers to the first
and one-past-the-end elements. You can also pass pointers to remove_if
too if the pointers are non-const.
I know that the word "iterator" is typedef'd from something for everyone of
these containers so I was curious if all these functions are valid.
SGI's online documentation on the STL, which see, organizes classes into
concepts. It's a useful way of thinking about the C++ Standard Library's
containers and algorithms, and answering questions like this.
On the same note, should I use "find_if" or "remove_if" for the following
template:
You should use std::remove_if. It might also be an idea to call your
template something other than remove_if, because everyone knows what
remove_if does, and this isn't it.

template <class AssociativeCont ainer, class Predicate>
inline void remove_if(Assoc iativeContainer & C, Predicate pred,
class associative_con tainer_tag)
{
typedef typename AssociativeCont ainer::iterator iterator;
iterator cur = c.begin();
const iterator last = c.end();

while ( (cur = std::find_if(cu r, last, pred)) != last)
{
iterator tmp = cur++;
c.erase(tmp);
}
}


In that final function parameter, the `class' keyword class is redundant
(unless the name associative_con tainer_tag is also in scope as an
identifier for something which is not a class or a template class, in
which case I give up). You declare an unnamed parameter of type
`associative_co ntainer_tag'.

Luckily, you don't need a tag in this instance, because an associative
container is a forward container.

Try

template <typename ForwardContaine r, typename Pred>
inline void erase_if (ForwardContain er & c, Pred & p)
{
c.erase (c.begin (), std::remove_if (c.begin (), c.end (), p);
}

You could provide a version that works for, say, a stack using partial
specialization, still without using a tag class. Having said that, I
hope you never feel the need to apply this algorithm to a stack.

Regards,
Buster

Jul 19 '05 #2
> > For example, if "find_if", "remove_if" , etc... valid for both lists,
deques,
vectors, sets, and maps?


These two functions take two objects of some forward iterator type as
arguments (the first forward iterator must be mutable). A container
which provides forward iterators is called a forward container.
Therefore, find_if can be used with any forward container. The same
applies to remove_if but the forward iterator type has to be a mutable
iterator type as well, so you have to have a mutable container.

All five of the containers you mention are forward containers, so yes,
you can use both functions with all of the containers. In addition, a
pointer is a random access iterator and so certainly a forward iterator,
so you can use find_if with an array by passing pointers to the first
and one-past-the-end elements. You can also pass pointers to remove_if
too if the pointers are non-const.

According to this article http://www.adtmag.com/joop/crarticle.asp?ID=850
you can't use "remove_if" or "remove" on a set or a map.
Is the article incorrect?
Jul 19 '05 #3
sks_cpp wrote:
For example, if "find_if", "remove_if" , etc... valid for both lists,

deques, vectors, sets, and maps?

These two functions take two objects of some forward iterator type as
arguments (the first forward iterator must be mutable). A container
which provides forward iterators is called a forward container.
Therefore, find_if can be used with any forward container. The same
applies to remove_if but the forward iterator type has to be a mutable
iterator type as well, so you have to have a mutable container.

All five of the containers you mention are forward containers, so yes,
you can use both functions with all of the containers. In addition, a
pointer is a random access iterator and so certainly a forward iterator,
so you can use find_if with an array by passing pointers to the first
and one-past-the-end elements. You can also pass pointers to remove_if
too if the pointers are non-const.


According to this article http://www.adtmag.com/joop/crarticle.asp?ID=850
you can't use "remove_if" or "remove" on a set or a map.
Is the article incorrect?


No, the article is correct, and so are you. So is SGI's documentation.
I'm mostly right (which is to say I'm wrong). All I failed to notice was
that the iterators passed to remove_if are required to be mutable, but
the iterators provided by associative containers are constant.

I can't see which was the question you don't already know the answer to,
but I'll be happy if I can help.

Buster

Jul 19 '05 #4
"Buster Copley" <bu****@none.co m> wrote in message
news:be******** **@newsg4.svr.p ol.co.uk...
No, the article is correct, and so are you. So is SGI's documentation.
I'm mostly right (which is to say I'm wrong). All I failed to notice was
that the iterators passed to remove_if are required to be mutable, but
the iterators provided by associative containers are constant.

I can't see which was the question you don't already know the answer to,
but I'll be happy if I can help.


I just read in the "Associate Containers" section that keys are immutable.
So, for sets the value_type is not mutable and for maps, the key part of the
value_type is not mutable.

Well, if REMOVE_IF is not valid for a set then COPY shouldn't be either
since for COPY function, you have to assign from one container to another.
Right?

template <class InputIterator, class OutputIterator>
OutputIterator copy(InputItera tor first, InputIterator last, OutputIterator
result);
<standard header algorithm>
Copy copies elements from the range [first, last) to the range [result,
result + (last - first)). That is, it performs the assignments *result =
*first, *(result + 1) = *(first + 1), and so on. Generally, for every
integer n from 0 to last - first, copy performs the assignment *(result + n)
= *(first + n). Assignments are performed in forward order, i.e. in order of
increasing n.

I know I am wrong here so someone please explain to me what I am missing.

Thanks.
Jul 19 '05 #5
On Sun, 06 Jul 2003 17:48:28 GMT, sks_cpp <sk*****@hotmai l.com> wrote:
"Buster Copley" <bu****@none.co m> wrote in message
news:be******** **@newsg4.svr.p ol.co.uk...
No, the article is correct, and so are you. So is SGI's documentation.
I'm mostly right (which is to say I'm wrong). All I failed to notice was
that the iterators passed to remove_if are required to be mutable, but
the iterators provided by associative containers are constant.

I can't see which was the question you don't already know the answer to,
but I'll be happy if I can help.


I just read in the "Associate Containers" section that keys are immutable.
So, for sets the value_type is not mutable and for maps, the key part of the
value_type is not mutable.

Well, if REMOVE_IF is not valid for a set then COPY shouldn't be either
since for COPY function, you have to assign from one container to another.
Right?


Yes. (Though copy doesn't work on containers, it works on iterators which
don't necessarily have a container behind them...)

copy isn't valid using a set iterator as the destination (you can use
an insert_iterator just fine, though).

--
Sam Holden

Jul 19 '05 #6

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

Similar topics

25
3798
by: Magnus Lie Hetland | last post by:
Is there any interest in a (hypothetical) standard graph API (with 'graph' meaning a network, consisting of nodes and edges)? Yes, we have the standard ways of implementing graphs through (e.g.) dicts mapping nodes to neighbor-sets, but if one wants a graph that's implemented in some other way, this may not be the most convenient (or abstract) interface to emulate. It might be nice to have the kind of polymorphic freedom that one has with,...
3
3469
by: fabio de francesco | last post by:
Hello, I have a couple of years of experience with C++. I started studying C++ syntax, then I read the B.Stroustrup's book, and eventually I went through the N.Josuttis' book on how to program with the C++ Standard Library. I am not a professional programmer however I would like to gain more knowledge of the language to get what could be considered the skills you'd expect from a professional developer ( I also know Unix
10
1503
by: Lord Labakudas | last post by:
Hi, I have a very simple question. Is int a predefined class in the standard C++ library. If yes, is it typedef'd as int somewhere ? I wish to know this because, I wish to create a class that has all the functionalities of int, but is also derived from another base class. For example, class Tuple: public Data, public int {
43
5032
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own libraries. I'm not sure if that's a good thing or not. AFAIK, most of Qt is compatable with the Standard Library. That is, QLT can interoperate with STL, and you can convert back and forth between std::string and Qt::QString, etc. Are there any...
104
5202
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a better way than to fill an array of range 0... RAND_MAX with pre-computed primes and using the output of rand() to index into it to extract a random prime.
22
2515
by: David Mathog | last post by:
One thing that keeps coming up in this forum is that standard C lacks many functions which are required in a workstation or server but not possible in an embedded controller. This results in a plethora of "don't ask here, ask in comp.x.y instead", for queries on functions that from the documentation available to the programmer appear to be part of the C language. I think largely because of this "least common denominator" C language...
85
4879
by: fermineutron | last post by:
Some compilers support __asm{ } statement which allows integration of C and raw assembly code. A while back I asked a question about such syntax and was told that __asm is not a part of a C standard. My question now is: Is there a chance that such statement will become a part of C standard in the future? In some cases using asm language is the best way to acomplish some small task, hence integration of C and asm would greatly enhence C,...
32
1889
by: subramanian100in | last post by:
Current ISO C++ standard is ISO C++ 1998. Am I correct ? When is the next standard expected ? What is the URL for knowing it ? Will it contain libraries for network programming as part of the standard library ?.(just as data structures and algorithms were included as part ISO C++ 1998 standard library). Kindly clarify.
126
6760
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard: http://www.cse.wustl.edu/~schmidt/ACE.html the same way that the STL (and subsequently BOOST) have been subsumed? Since it already runs on zillions of platforms, they have obviously worked most of the kinks out of the generalized threading and processes idea (along with many...
0
9718
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
9596
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
10364
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
10109
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...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5545
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...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3849
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.