473,779 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::map

2 questions:

Given a map defined as

std::map<int,st ring> stringmap;

//How do you access the data_type (string) via an iterator?

std::string spoo("doh");

stringmap.inser t(std::make_pai r(1,spoo));
// I tried this:
for(std::map<in t,string> ::iterator it = stringmap.begin (); it !=
stringmap.end() ; ++it)
std::cout << *it;
But this doesn't compile with error that the operator+ has not been defined
....
I think the problem is that the iterator returns a pair. Is it possible to
do this?

I know I can do:
if(stringmap.fi nd(1) != stringmap.end() ) std::cout << stringmap[1];
2nd question:

This doesn't work with BCB6 (causes a compile error about casting a pair to
an int???)

//stringmap is defined as a private class member
const std::string classname::gets tring(size_t index) const { return
stringmap[index];}

but this compiles fine and seems to work with no problems:

const std::string classname::gets tring(size_t index) { return
stringmap[index];}

Is this correct? Why can I not define this function as const? It's not
changing any
member data.

Thanks.

Jul 19 '05
24 17330
Smeckler wrote:
Would you care to be more specific about your objection to my code?


First, this has undefined behaviour. (Dereferencing a 0 Pointer)

Second, how do you test the returned reference for 0??
You cannot. So somebody ends up with a dangling reference. And probably
crashes a while later when he tries to use it...

Christoph

Jul 19 '05 #21
On Tue, 9 Sep 2003 10:20:27 +0100, "Smeckler"
<sm*********@ho tSPAMmail.com> wrote:
> Easy. And, since this is the STL we're talking about, why not create a
> handy template function :)
>
> template <typename TypeName> TypeName& GetNullReferenc e()
> {
> return *(TypeName*)NUL L;
> }
>
>


I hope you are joking. If not, you need to learn some C++.

-Kevin

Kevin,

Would you care to be more specific about your objection to my code?


It has undefined behaviour. Dereferencing a null pointer is always a
bug. In other words, in the language spec, there is no such thing as a
null reference, since in order to create one, you have to go outside
the language spec.

In practice, the code will crash on some platforms, while on others it
will return what you think of as a "null reference".

Tom
Jul 19 '05 #22

"Duane Hebert" <sp**@flarn.com > wrote in message
news:I_******** ***********@web er.videotron.ne t...

I prefer to use the dot operator when referencing an object that's not a
pointer. It's
a matter of taste and not worth an argument. Both ways work. To me, it
wasn't intuitive
that (*it).first would work.


The whole point is it's supposed to look like a pointer. Otherwise, you
wouldn't have to go around your elbow to get to your butt to dereference it,
thereby forcing it to look like a reference just because that's how you want
it to look.
Jul 19 '05 #23

"Duane Hebert" <sp**@flarn.com > wrote in message
news:I_******** ***********@web er.videotron.ne t...

I prefer to use the dot operator when referencing an object that's not a
pointer. It's
a matter of taste and not worth an argument. Both ways work. To me, it
wasn't intuitive
that (*it).first would work.


Or to put it another way, if it were just a regular reference object, you
would access it like
it.
instead of like
(*it).
So it's not just a matter of preference in this case, it's a matte of how
the thing was designed to be used - i.e., like a pointer.
Jul 19 '05 #24
Smeckler wrote:
Easy. And, since this is the STL we're talking about, why not create a
handy template function :)

template <typename TypeName> TypeName& GetNullReferenc e()
{
return *(TypeName*)NUL L;
}


I hope you are joking. If not, you need to learn some C++.

-Kevin


Kevin,

Would you care to be more specific about your objection to my code?


I could, but I think I'll let Herb Sutter do it instead:

http://www.gotw.ca/conv/002.htm

To summarize: You cannot have a null reference. You cannot dereference a
null pointer, under any circumstances.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #25

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

Similar topics

3
30085
by: Woodster | last post by:
I have declared the following std::map<std::string, std::string> myMap; to pass myMap to functions should I be declaring functions as: void function(std::map<std::string, std::string>); or is there a preferred/better method of doing this?
1
1872
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
2
3405
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string, MathFuncPtr> predefinedFunctions; // lots of other stuff void makeDictionary(){ predefinedFunctions=&F::f_sin(); } };
1
3554
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my program uses two MFC classes: CRect and CPoint which represents Rectangle and Point concepts (as usual) and a user
19
6165
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
3
3716
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I can't think of a good fix, or even why it broke. The problem In one of my CFormView derived classes I have a member variable of the type...
1
6479
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed under Visual Studio .NET 2003 in a Win32 Console Project // VectorInsert.cpp : Defines the entry point for the console application / #include "stdafx.h #include "VectorInsert.h #ifdef _DEBU
13
9678
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
2
5379
by: digz | last post by:
Hi, I am trying to write a program which has two threads one of them write to a map , and the other one deletes entries based on a certain criterion.. first I cannot get the delete portion to work , what am i missing here. also is it possible/correct that the removeKeyValue function acquire the mutex lock only during the call to map.erase(), and not the during the whole iteration process as i have done here( i logically felt it was...
8
4077
by: mveygman | last post by:
Hi, I am writing code that is using std::map and having a bit of an issue with its performance. It appears that the std::map is significantly slower searching for an element then a sequential search in a vector. Has anyone run into this before?
0
9636
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
9474
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
10139
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...
1
10075
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
9931
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
5373
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
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
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.