473,473 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

associative containers and algorithms

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
Jun 27 '08 #1
3 1521
su**************@yahoo.com, India wrote:
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.
Jun 27 '08 #2
S S
On May 6, 10:14 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
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.
Jun 27 '08 #3
On 2008-05-06 01:14:12 -0400, "su**************@yahoo.com, India"
<su**************@yahoo.comsaid:
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)

Jun 27 '08 #4

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

Similar topics

2
by: Ney André de Mello Zunino | last post by:
Hello. A non-modifying algorithm I implemented uses two associative containers from STL: set and map. The elements on those containers are supposed to refer to actual elements which lie on...
1
by: Dave | last post by:
Hello all, Why is the term "associative" containter used to describe std::set<>, std::map<>, std::multiset<> and std::multimap<>? I always had the impression that it is related to the fact that...
7
by: Matthias Käppler | last post by:
Hi, I have the following problem: I want to store file objects in a container 8for now, it's not important how they look like). Now, on the one hand I need to be able to randomly pick files...
2
by: bob | last post by:
Hi, Given: 1) Custom containers that have nothing to do with vector, deque, list, map etc, 2) a custom version of new and delete defined for these containers, customNew and customDelete,...
2
by: utab | last post by:
Hi there, Can you please explain me why there are sequential and associative containers in c++ on some examples and briefly specify the advantages of using one over the other(and also from the...
1
by: yonil | last post by:
I hope this is the correct group for this... I'm currently implementing the TR1 associative containers according to specification found in...
3
by: Johs | last post by:
I have implemented a red-black tree based on the description in Introduction to Algorithms Cormen section 13. But I would like to make all iterator operations to take O(1) time in worst case. Is...
5
by: desktop | last post by:
set, map, multiset and multimap are all associative containers. But what does the word "associative" have to do with these 4 containers?
4
by: Morten | last post by:
Hi. Is there anybody who knows a C (not c++) library containing implementations of associative containers (stack, queue, hash table, linked list). It would be great if the performance of...
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
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...
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...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.