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

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 AssociativeContainer, class Predicate>
inline void remove_if(AssociativeContainer& C, Predicate pred,
class associative_container_tag)
{
typedef typename AssociativeContainer::iterator iterator;
iterator cur = c.begin();
const iterator last = c.end();

while ( (cur = std::find_if(cur, last, pred)) != last)
{
iterator tmp = cur++;
c.erase(tmp);
}
}
Jul 19 '05 #1
5 3020
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_iterator' 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 AssociativeContainer, class Predicate>
inline void remove_if(AssociativeContainer& C, Predicate pred,
class associative_container_tag)
{
typedef typename AssociativeContainer::iterator iterator;
iterator cur = c.begin();
const iterator last = c.end();

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


In that final function parameter, the `class' keyword class is redundant
(unless the name associative_container_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_container_tag'.

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

Try

template <typename ForwardContainer, typename Pred>
inline void erase_if (ForwardContainer & 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.com> wrote in message
news:be**********@newsg4.svr.pol.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(InputIterator 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*****@hotmail.com> wrote:
"Buster Copley" <bu****@none.com> wrote in message
news:be**********@newsg4.svr.pol.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
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.)...
3
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...
10
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...
43
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...
104
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...
22
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...
85
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...
32
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...
126
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:...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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,...

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.