472,374 Members | 1,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

can I re reference items in a map

I have class CMyClass to which I create objects of and store a count of them
in a stl map. (stlport)

like so

typedef map<CMyClass, unsigned, myclass_less> Results_Map;
....
Results_Map m_Map;
....
....
CMyClass mc(...);
m_Map[mc]++;
....
How can I get access to the same mc object I just added to the map? In other
words I'm trying to do something like

CMyClass& mcRef = *m_Map[mc];

If thats possible.

thanks

Feb 7 '06 #1
7 1463
Whybother wrote:
I have class CMyClass to which I create objects of and store a count of them
in a stl map. (stlport)

like so

typedef map<CMyClass, unsigned, myclass_less> Results_Map;
...
Results_Map m_Map;
...
...
CMyClass mc(...);
m_Map[mc]++;
...
How can I get access to the same mc object I just added to the map? In other
words I'm trying to do something like

CMyClass& mcRef = *m_Map[mc];

If thats possible.


I think you're confused with how 'map' works. You store _unsigned_ values
there. You make your objects the _keys_ for those unsigned values. Keys
get _copied_. If you need to find what key a particular unsigned value
has, you can _search_ for that value and then look at the key through the
iterator you get back...

Indexing by an object will get you the 'unsigned' part of the map. For
example

m_Map[mc]

results in a reference to the unsigned value corresponding to the counter
of the objects that have the same "value" as 'mc'. But if you have 'mc',
why do you need a reference to it?

IOW, based on what you wrote here and the questions you asked, I have no
clues as to what you're trying to do. What is it you're attempting to
accomplish? Perhaps 'map' is not the best solution, or perhaps you need
to define it differently...

V
--
Please remove capital As from my address when replying by mail
Feb 7 '06 #2
"Whybother" <fu**@fuck.net> wrote in message
news:wv********************@comcast.com...
I have class CMyClass to which I create objects of and store a count of
them in a stl map. (stlport)

like so

typedef map<CMyClass, unsigned, myclass_less> Results_Map;
...
Results_Map m_Map;
...
...
CMyClass mc(...);
m_Map[mc]++;
Have you defined an operator++() for type 'CMyClass'?
...
How can I get access to the same mc object I just added to the map? In
other words I'm trying to do something like

CMyClass& mcRef = *m_Map[mc];
If thats possible.


CMyClass& mcRef = m_Map.find(mc)->first;

(This assumes that the map contains a key with value == mc.
If it's possible that it doesn't, you should compare the return
value of 'm_Map.find()' with 'm_map.end()', and only dereference
the iterator if they're not equal)
-Mike
Feb 7 '06 #3
Mike Wahler wrote:
"Whybother" <fu**@fuck.net> wrote in message
news:wv********************@comcast.com...
I have class CMyClass to which I create objects of and store a count of
them in a stl map. (stlport)

like so

typedef map<CMyClass, unsigned, myclass_less> Results_Map;
...
Results_Map m_Map;
...
...
CMyClass mc(...);
m_Map[mc]++;

Have you defined an operator++() for type 'CMyClass'?


Why would it be needed? 'm_Map[mc]' yields 'unsigned&'. ++ is defined
for the unsigned, isn't it?
[...]


V
--
Please remove capital As from my address when replying by mail
Feb 7 '06 #4
I'm thinking backwards. I am trying to store a list of reference numbers
within the CMyClass objects. So when I increase the count ala >>
m_Map[mc]++; I'd like to also store the reference number. something like

m_Map[mc]++;
CMyClass& refMyClass = m_Map[mc];
refMyClass.AddReferenceNumber(x);

so later when I dump it out each MyClass object will contain the list of
reference numbers.

I originally tried this

mc.AddReferenceNumber(x);
m_Map[mc]++;

and in the copy constructor I was doing this

void CMyClass::copy (const CMyClass& bs)
{
...
m_mapRefNumbers.assign(bs.m_mapRefNumbers.begin(),
bs.m_mapRefNumbers.end());
...
}

but when I dumped the map each CMyClass objects m_mapRefNumbers only
contained one reference not the culmulative collection of all the ref
numbers.
I know one solution would be to have a struct

typedef struct
{
int iCount;
ReferenceMap m_RefMap;
} MyClassRefMap

then have
typedef map<CMyClass, MyClassRefMap, myclass_less> Results_Map;

but I'm unclear as to why my other solution wasn't correct.

thanks
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:aU**************@newsread1.mlpsca01.us.to.ver io.net... Whybother wrote:
I have class CMyClass to which I create objects of and store a count of
them in a stl map. (stlport)

like so

typedef map<CMyClass, unsigned, myclass_less> Results_Map;
...
Results_Map m_Map;
...
...
CMyClass mc(...);
m_Map[mc]++;
...
How can I get access to the same mc object I just added to the map? In
other words I'm trying to do something like

CMyClass& mcRef = *m_Map[mc];

If thats possible.


I think you're confused with how 'map' works. You store _unsigned_ values
there. You make your objects the _keys_ for those unsigned values. Keys
get _copied_. If you need to find what key a particular unsigned value
has, you can _search_ for that value and then look at the key through the
iterator you get back...

Indexing by an object will get you the 'unsigned' part of the map. For
example

m_Map[mc]

results in a reference to the unsigned value corresponding to the counter
of the objects that have the same "value" as 'mc'. But if you have 'mc',
why do you need a reference to it?

IOW, based on what you wrote here and the questions you asked, I have no
clues as to what you're trying to do. What is it you're attempting to
accomplish? Perhaps 'map' is not the best solution, or perhaps you need
to define it differently...

V
--
Please remove capital As from my address when replying by mail

Feb 7 '06 #5
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:a6***************@newsread1.mlpsca01.us.to.ve rio.net...
Mike Wahler wrote:
typedef map<CMyClass, unsigned, myclass_less> Results_Map;
...
Results_Map m_Map;
...
...
CMyClass mc(...);
m_Map[mc]++;

Have you defined an operator++() for type 'CMyClass'?


Why would it be needed? 'm_Map[mc]' yields 'unsigned&'. ++ is defined
for the unsigned, isn't it?


Ack! I must have looked at it backwards. Thanks for the
correction.

-Mike
Feb 7 '06 #6
In article <Lu******************************@comcast.com>,
"Whybother" <fu**@fuck.net> wrote:
I'm thinking backwards. I am trying to store a list of reference numbers
within the CMyClass objects. So when I increase the count ala >>
m_Map[mc]++; I'd like to also store the reference number. something like

m_Map[mc]++;
CMyClass& refMyClass = m_Map[mc];
refMyClass.AddReferenceNumber(x);

so later when I dump it out each MyClass object will contain the list of
reference numbers.

I originally tried this

mc.AddReferenceNumber(x);
m_Map[mc]++;

and in the copy constructor I was doing this

void CMyClass::copy (const CMyClass& bs)
{
...
m_mapRefNumbers.assign(bs.m_mapRefNumbers.begin(),
bs.m_mapRefNumbers.end());
...
}

but when I dumped the map each CMyClass objects m_mapRefNumbers only
contained one reference not the culmulative collection of all the ref
numbers.
I know one solution would be to have a struct

typedef struct
{
int iCount;
ReferenceMap m_RefMap;
} MyClassRefMap

then have
typedef map<CMyClass, MyClassRefMap, myclass_less> Results_Map;


but I'm unclear as to why my other solution wasn't correct.


I've read both your post and I'm still not getting it. How about you
post something like "I have this (describe your starting objects,) and I
want to end up with this (describe desired results.)"

It sounds like you have a number of MyClass objects, and you want to add
a list of values to them? But that wouldn't have anything to do with a
map, a vector or list would be fine...
Feb 7 '06 #7
Whybother wrote:
I have class CMyClass to which I create objects of and store a count of them
in a stl map. (stlport)

like so

typedef map<CMyClass, unsigned, myclass_less> Results_Map;
...
Results_Map m_Map;
...
...
CMyClass mc(...);
m_Map[mc]++;
...
How can I get access to the same mc object I just added to the map? In other
words I'm trying to do something like

CMyClass& mcRef = *m_Map[mc];

If thats possible.

thanks


I think you want to do something like the following:

typedef map <CMyClass, unsigned, myclass_less> Results_Map;
typedef Results_Map::iterator rmIter;
typedef Results_Map:: value_type rmVal;

Results_Map m_Map;
CMyClass mc(...);
rmIter it = m_Map.find(mc);
if (it != m_Map.end()) // key found
++it->second;
else // new key, assign initial data value = 1
it = m_Map.insert(rmVal(mc,1)).first;
const CMyClass& mcRef = it->first;
// do stuff with mcRef

Note that I made mcRef a const& since the keys of a map should be constant.

-Mark
Feb 8 '06 #8

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

Similar topics

4
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back...
3
by: UGH | last post by:
I am looping the grid rows to set the values in the dropdownlists. I keep getting a run time error "Object reference not set to an instance of an object." The error occurs on line where it says...
6
by: freddy | last post by:
I would like to load a path to a file when the form loads like this: public void Form1_Load(object sender, System.EventArgs e) { string path = "G:\\\\Freddy\\vb.net\\c#\\football.txt";...
3
by: PatricQ | last post by:
I use VS.NET and yes I'm a beginner in ASP.NET. Here is the problem: in "%webroot%\components" is the folowing vb code file: framework.vb ---------------------------------------- Imports...
0
by: Kaimar Seljamäe | last post by:
Hi, I have to create a web service client which uses SOAP encoding but does not use "multi-reference" values (see http://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383513 item 10). If I...
4
by: tg.foobar | last post by:
i'd like to do the following, but i don't think it's possible. can you help me find a way to do this, or maybe a better way to write the code? I have a list of items that need to be modified...
3
by: Arjen | last post by:
Hi, I have a public class 'settings' with a public static void 'loadsettings'. Inside 'loadsettings' I go to public static 'loadpagesettings'. Inside this function I have a problem. 1. I...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
4
by: yogarajan | last post by:
hi i had developed pop3 Account mail view in my web page but i got error in (System.NullReference Exception: Object reference not set) NetStrm.Write(szData, 0, szData.Length); - this line ...
2
by: Dave | last post by:
The problem is with my listbox on the form. I am trying to run 2 threads simulataneously an update two different listbox controls but my functions are complaining that the listbox doesn't have an...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.