473,769 Members | 3,755 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 17327
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
30084
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
1869
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
3553
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
6164
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
3715
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
9677
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
4076
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
9583
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
10210
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...
0
10039
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
9860
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
8869
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
5297
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
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3955
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
3
2814
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.