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. 24 17272
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
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/
> > 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?
> > 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 :)
"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".
"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
"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?)
> > // 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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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?
|
by: Antti Granqvist |
last post by:
Hello!
I have following object relations:
Competition 1--* Category 1--* Course
1
|
|
*
Course
|
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(); }
};
|
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
|
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...
| |
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...
|
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...
|
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
|
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...
|
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?
|
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...
| |
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...
|
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. ...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
| |