473,805 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ 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 1541
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.comwro te:
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>::itera tor 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.comsai d:
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
2384
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 another, bigger container. I had the definition of my auxiliary containers based on pointers, but considered using iterators instead. I know iterators might become invalid upon certain operations, but as I said, the algorithm is non-modifying. This...
1
1368
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 elements in these containers are sorted. However, the (soon-to-be standard) hash-based containers are also considered associative. In these containers, element order is determined purely by the hash function and the insertion order, not by any...
7
1706
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 from the container as fast as possible. So far I have been using std::map to store the files, whereas the key was a unique file ID. That given, I could pick random files from the container in O(logn). On the other hand, I need functionality to sort...
2
1602
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, say. When an item is to be inserted/removed into/from these custom containers, customNew and customDelete is called.
2
5747
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 point of performance as well, I know that it is hard to answer because this is dependent on the application that you would like to develop :-)) ) Thanks for all comments in advance.
1
1805
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 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1745.pdf. Now I'm not sure how to interpret a requirement relating to exception safety. It says in 6.3.1.1: "For unordered associative containers, if an exception is thrown by any operation other than the container's hash function from within...
3
1566
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 this even possible and if so does anyone know of any articles websites dealing with this optimzation?
5
1883
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
1589
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 algorithms where high. Regards Morten.
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
10363
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...
1
7649
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
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
5544
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
3847
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.