Connecting Tech Pros Worldwide Help | Site Map

associative containers and algorithms

subramanian100in@yahoo.com, India
Guest
 
Posts: n/a
#1: Jun 27 '08
I am copying the following lines as it is, from Stanley Lippman's C++
Primer 4th edition, page 418(First paragraph).

It says:
"Although the map and set types provide bidirectional iterators, we
can use only a subset of the algorithms on associative containers. The
problem is that key in an associative container is const. Hence, any
algorithm that writes to elements in the sequence cannot be used on an
associative container. We may use iterators bound to associative
containers only to supply arguments that will be read."

Here, consider the line "any algorithm that writes to elements in the
sequence cannot be used on an associative container".

Consider the code fragment:

typedef set<ints_type;
s_type s1;
s_type s2;

// store some values into s1 and s2.

s_type dest;

merge(s1.begin(), s1.end(), s2.begin(), s2.end(),
inserter(dest, dest.end()));

s_type temp;

copy(s1.begin(), s1.end(), inserter(temp, temp.end()));

Here I am able to write merged elements into dest and copy elements
into temp. Can these operations be considered as "writing elements
into associative container" ? Or, if the above stated line from the
book refers to some other algorithms ? If so, please give me one or
two such algorithm names, for my understanding purpose.

My question is: I am to write elements from the input sequence into
insert_iterator< set<int

Kindly clarify.

Thanks
V.Subramanian
Ian Collins
Guest
 
Posts: n/a
#2: Jun 27 '08

re: associative containers and algorithms


subramanian100in@yahoo.com, India wrote:
Quote:
I am copying the following lines as it is, from Stanley Lippman's C++
Primer 4th edition, page 418(First paragraph).
>
It says:
"Although the map and set types provide bidirectional iterators, we
can use only a subset of the algorithms on associative containers. The
problem is that key in an associative container is const. Hence, any
algorithm that writes to elements in the sequence cannot be used on an
associative container. We may use iterators bound to associative
containers only to supply arguments that will be read."
>
Here, consider the line "any algorithm that writes to elements in the
sequence cannot be used on an associative container".
>
>
Here I am able to write merged elements into dest and copy elements
into temp. Can these operations be considered as "writing elements
into associative container" ?
"writes *to* elements in the sequence" != "writing elements *into*
associative container"

--
Ian Collins.
S S
Guest
 
Posts: n/a
#3: Jun 27 '08

re: associative containers and algorithms


On May 6, 10:14 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Quote:
I am copying the following lines as it is, from Stanley Lippman's C++
Primer 4th edition, page 418(First paragraph).
>
It says:
"Although the map and set types provide bidirectional iterators, we
can use only a subset of the algorithms on associative containers. The
problem is that key in an associative container is const. Hence, any
algorithm that writes to elements in the sequence cannot be used on an
associative container. We may use iterators bound to associative
containers only to supply arguments that will be read."
>
Here, consider the line "any algorithm that writes to elements in the
sequence cannot be used on an associative container".
>
Consider the code fragment:
>
typedef set<ints_type;
s_type s1;
s_type s2;
>
// store some values into s1 and s2.
>
s_type dest;
>
merge(s1.begin(), s1.end(), s2.begin(), s2.end(),
inserter(dest, dest.end()));
>
s_type temp;
>
copy(s1.begin(), s1.end(), inserter(temp, temp.end()));
>
Here I am able to write merged elements into dest and copy elements
into temp. Can these operations be considered as "writing elements
into associative container" ? Or, if the above stated line from the
book refers to some other algorithms ? If so, please give me one or
two such algorithm names, for my understanding purpose.
>
My question is: I am to write elements from the input sequence into
insert_iterator< set<int
>
Kindly clarify.
>
Thanks
V.Subramanian
"merge" and "copy" is not modifying the contents of 'set' OR they are
not modifying the key. They are just merging the new contents into the
tree (internally set/map are tree implementation).
Hence not modifying the contents means, you can not do something like
this,
set<int>::iterator it = s_type.find(10); // suppose you found.
*it = 20; // not allowed.
Pete Becker
Guest
 
Posts: n/a
#4: Jun 27 '08

re: associative containers and algorithms


On 2008-05-06 01:14:12 -0400, "subramanian100in@yahoo.com, India"
<subramanian100in@yahoo.comsaid:
Quote:
I am copying the following lines as it is, from Stanley Lippman's C++
Primer 4th edition, page 418(First paragraph).
>
It says:
"Although the map and set types provide bidirectional iterators, we
can use only a subset of the algorithms on associative containers. The
problem is that key in an associative container is const. Hence, any
algorithm that writes to elements in the sequence cannot be used on an
associative container. We may use iterators bound to associative
containers only to supply arguments that will be read."
>
While this explanation is technically correct, it's at the wrong level
of detail. The problem is that modifying elements in an associative
container could disrupt the ordering of the container. So you aren't
allowed to do it. In the library's interface that's enforced by making
the key const and by providing only iterators that don't support
modifying the elements that they point to.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Closed Thread