473,903 Members | 4,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 #1
24 17364
Duane Hebert wrote:
// I tried this:
for(std::map<in t,string> ::iterator it = stringmap.begin (); it !=
stringmap.end() ; ++it)
std::cout << *it;
std::cout << it->second;

should do the trick.

Btw I recommend that you use a const_iterator instead of an
iterator.

I know I can do:
if(stringmap.fi nd(1) != stringmap.end() ) std::cout << stringmap[1];
Brr, this is terribly inefficient. Why dont you store the
returnvalue of find?

if((it = stringmap.find( 1)) != stringmap.end() )
std::cout << it->second;

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.


Because operator[] is not const. It might change the
contents of your stringmap. The map is const in this
function, so you are not allowed to call it.

Use find instead.

hope this helps,

Christoph

Jul 19 '05 #2
Duane Hebert wrote in news:YY******** **********@wagn er.videotron.ne t:
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;
Change this to it->second; if you want the key do it->first.


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];


This should work fine stringmap[i] should return a std::string &
2nd question:

This doesn't work with BCB6 (causes a compile error about casting a
pair to an int???)
I don't really understand this, maybe you should post a complete
example that doesen't compile.

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

A return of std::string const & would be more effecient here.

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.


Yes this is correct std::map<>::ope rator[] is not defined as a
const-member function, this is because the non-const version
will insert a new element into the map if it doesn't exist.

std::string classname::gets tring( int index ) const
{
std::map<int,st ring>::iterator ptr =
stringmap.find( index )
:

if ( ptr != stringmap.end() ) return ptr->second;

// return std::string( "< some default>" );
//throw std::runtime_er ror( "Item Not Found" );

// the choice is your's.
}
HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #3
> > 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.


Because operator[] is not const. It might change the
contents of your stringmap. The map is const in this
function, so you are not allowed to call it.


But why doesn't map define a const operator[] in addition to the non const
one?
Jul 19 '05 #4
> > But why doesn't map define a const operator[] in addition to the non
const
one?

What would it do if the key was not present in the map?


Errrr...do nothing and return a reference to NULL :)
Jul 19 '05 #5

"Duane Hebert" <sp**@flarn.com > wrote in message
news:YY******** **********@wagn er.videotron.ne t...
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;


So you've figured out that you need to insert pairs, so the thing in the map
that the iterator is pointing to is a pair. You also said you want only the
string, not the whole pair. *it would be the whole pair. In a pair, there
is a "first" and "second". You want to point to the "second".
Jul 19 '05 #6

"Smeckler" <sm*********@ho tSPAMmail.com> wrote in message
news:bj******** ***********@new s.demon.co.uk.. .
But why doesn't map define a const operator[] in addition to the non const one?

What would it do if the key was not present in the map?


Errrr...do nothing and return a reference to NULL :)

And how do you return a _reference_ to NULL?

Chris
Jul 19 '05 #7

"Chris Theis" <Ch************ *@nospam.cern.c h> wrote in message news:bj******** **@sunnews.cern .ch...

"Smeckler" <sm*********@ho tSPAMmail.com> wrote in message
news:bj******** ***********@new s.demon.co.uk.. .
> But why doesn't map define a const operator[] in addition to the non

const
> one?
>
What would it do if the key was not present in the map?


Errrr...do nothing and return a reference to NULL :)

And how do you return a _reference_ to NULL?

That was a joke, son. (Didn't you see the smilely?)
Jul 19 '05 #8
> > // I tried this:
for(std::map<in t,string> ::iterator it = stringmap.begin (); it !=
stringmap.end() ; ++it)
std::cout << *it;
Change this to it->second; if you want the key do it->first.


Thanks. I stumbled on that myself. I also found that
std::cout (*it).second; works as well. This was what I tried
originally but I missed the () .

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


This should work fine stringmap[i] should return a std::string &


That's correct but if I want to access each element of the map with a for
loop
it requires that I know the min and max keys and try all of them. That's
why
I wanted to get the iterator to work. (also to figure it out :-)

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


A return of std::string const & would be more effecient here.


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.


Yes this is correct std::map<>::ope rator[] is not defined as a
const-member function, this is because the non-const version
will insert a new element into the map if it doesn't exist.

std::string classname::gets tring( int index ) const
{
std::map<int,st ring>::iterator ptr =
stringmap.find( index )
:

if ( ptr != stringmap.end() ) return ptr->second;


This didn't exactly work on my compiler. Threw the same error as before.
I had to make ptr a const_iterator,
then it worked fine. Not sure why since find() returns an iterator,
but thanks for the help.
HTH


It does. Thanks.
Jul 19 '05 #9
Duane Hebert wrote:
std::string classname::gets tring( int index ) const
{
std::map<int,st ring>::iterator ptr =
stringmap.find( index )
:

if ( ptr != stringmap.end() ) return ptr->second;


This didn't exactly work on my compiler. Threw the same error as before.
I had to make ptr a const_iterator,
then it worked fine. Not sure why since find() returns an iterator,
but thanks for the help.


No, because the stringmap is const in this function find
returns a const_iterator in this case.

Christoph

Jul 19 '05 #10

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

Similar topics

3
30100
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
1885
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
2
3411
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
3559
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
6202
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
3721
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
6487
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
9687
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
5381
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
4086
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
10003
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
10882
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
10988
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
10504
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
9692
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
8055
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
5897
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
6099
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4312
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.