473,623 Members | 2,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how does map iterator actually reference underlying object?

I have code such as the following:

map<int, stringstringMap ;

stringMap.inser t(map<int,strin g>::value_type( 1, "hello"));

map<int, string>::iterat or iter;

iter = stringMap.find( 1);

iter->second = "goodbye";

I know after experimentation that when I use the iterator to change
the value of second, that I am actually manipulating the underlying
object. So the next time I call find, it will reference "goodbye".

My question is, how does it accomplish this? Is this a hard and fast
rule? Can I assume that the find member function for maps will not
copy-by-value and always allow me to manipulate the underlying object
through the interator, even if it were things like pointers, etc?

Thanks,

Ken Jackson

Jun 10 '07 #1
2 3805
On 2007-06-10 11:41:24 -0700, kj**********@gm ail.com said:
I have code such as the following:

map<int, stringstringMap ;

stringMap.inser t(map<int,strin g>::value_type( 1, "hello"));

map<int, string>::iterat or iter;

iter = stringMap.find( 1);

iter->second = "goodbye";

I know after experimentation that when I use the iterator to change
the value of second, that I am actually manipulating the underlying
object. So the next time I call find, it will reference "goodbye".

My question is, how does it accomplish this?
By implementing operator-to return a pointer to the actual pair
contained in the map.
Is this a hard and fast
rule? Can I assume that the find member function for maps will not
copy-by-value and always allow me to manipulate the underlying object
through the interator, even if it were things like pointers, etc?
Yes, you can rely on this. Just as you can for any of the standard
container's non constant iterators.
--
Clark S. Cox III
cl*******@gmail .com

Jun 10 '07 #2
On Jun 10, 8:41 pm, kjackson....@gm ail.com wrote:
I have code such as the following:
map<int, stringstringMap ;
stringMap.inser t(map<int,strin g>::value_type( 1, "hello"));
map<int, string>::iterat or iter;
iter = stringMap.find( 1);
iter->second = "goodbye";
I know after experimentation that when I use the iterator to change
the value of second, that I am actually manipulating the underlying
object. So the next time I call find, it will reference "goodbye".
Correct.
My question is, how does it accomplish this?
By having the -return a reference to the underlying data
structure.
Is this a hard and fast rule?
For the standard containers, yes. In fact, the requirements of
iterators require this for all iterators except input and output
iterators.
Can I assume that the find member function for maps will not
copy-by-value and always allow me to manipulate the underlying object
through the interator, even if it were things like pointers, etc?
The find member function returns an iterator, and *that* is
returned by value. But the dereference operators of the
iterator (* and ->) are required to return references to the
actual data element in the container. For all standard
containers.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 10 '07 #3

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

Similar topics

7
3266
by: Rene Pijlman | last post by:
Section 6.5 "What is delegation?" of the FAQ says: "Python programmers can easily implement delegation. For example, the following class implements a class that behaves like a file but converts all written data to uppercase: class UpperOut: def __init__(self, outfile): self.__outfile = outfile def write(self, s):
5
2074
by: Leif K-Brooks | last post by:
Is there a word for an iterable object which isn't also an iterator, and therefor can be iterated over multiple times without being exhausted? "Sequence" is close, but a non-iterator iterable could technically provide an __iter__ method without implementing the sequence protocol, so it's not quite right.
0
3391
by: sks_cpp | last post by:
I am trying to wrap the map iterator for keys and values. However, I seem to run into problems with the values for const_iterator - it works for all other combinations. Below I list my code and the compiler error. Please help if possible. Thanks in advance. // Compiler error ra:/home/ssangapu/personal/C++/STL/mapIter-738>g++ main.cc utlMapIterator.h: In method `class Object *& ValueIterator<_Rb_tree_iterator<pair<const int,Object...
20
3218
by: Steven T. Hatton | last post by:
I just read this in the description of how C++ is supposed to be implemented: "All external object and function references are resolved. Library components are linked to satisfy external references to functions and objects not defined in the current translation. All such translator output is collected into a program image which contains information needed for execution in its execution environment." What I'm wondering is what exactly...
5
2090
by: Barry Anderberg | last post by:
I'm using a tool by Sci-Tech called the .NET Memory Profiler. We have a massive .NET/C# application here and it has been exhibiting memory leak behavior for some time. Attempting to remedy the problems I am employing the aforementioned software in trying to track down the problems. After an hour or so of program execution, a snapshot of the managed heap shows 27,025 BinaryReader objects as having been garbage collected but never...
16
2576
by: mailforpr | last post by:
How do I do that? The thing is, the only information I have about the iterator is the iterator itself. No container it is belonging to or anything. Like template<Iteratorvoid totally_isolated(Iterator& it) { //how do I find out if it points to the end node? }
3
2795
by: Belebele | last post by:
I have an Element class which is abstract and I would like to have an object of the Iterator class to iterate over a range of elements. I would like to use std::for_each to instrument the iteration, which forces me to have certain interface on the Iterator class. struct Element { virtual ~Element() = 0; };
7
3829
by: Max Odendahl | last post by:
Hi, my own declared class has a stl::list as a member variable. I now need access to those from the outside. Is a custom iterator for my class the best solution for this and how to do this? I would like to sort the list according to different criteria before, so I would like to offer different iterators, which then sort first based on the type of iterator constructed. Any code sample or link would be nice.
2
13050
by: Lambda | last post by:
The code is simple: // Token.h #ifndef TOKEN_H #define TOKEN_H #include <vector> #include <string> class Token
0
8217
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
8661
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8312
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7132
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
5559
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
4067
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
4153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1467
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.