|
-
January 3rd, 2012, 05:38 PM
#1
std::map proper use?
Environment Windows XP Pro, Visual Studio, C++, MFC
After examining some code I use to put data in a map I have become concerned that I may be inserting data improperly. The map for parameters is defined as:
Code:
struct td_parameter_definitions
{
CString name;
unsigned ing tag;
...
};
typedef std::map< td_parameter_key, td_parameter_definitions > td_parameter_map;
The parameter map is within a message structure. The address of the message structure is handed to a method that reads a text file and fills in data. It creates a local pointer to the parameter map and sets fields within the parameter structure as follows:
Code:
Read_parameter_Definitions( st_msg_data *p_msg_data )
{ ...
// make a short hand name for the parameter map
td_parameter_map *p_parm_map = p_msg_data->parameter_map;
parameter_number = <an integer value>;
...
// cs_t1 is filled in by some additional code not shown here.
( *p_parm_map)[parameter_number].name = cs_t1;
// etc
... }
My problem is that I realized that I was not explicitly adding entry [parameter_number] to the map. The code just jumped up and starting putting data in that entry as though it already exists. I am finding that rather worrying.
Since the map is defined as a map of structures and not defined as a map of pointers, does the simple act of referencing entry [1] (for example) create that entry. If so, then is it OK for the code to proceed to set various fields of that entry.
Is that a back door to creating an entry in a map that I should not use?
-
January 3rd, 2012, 06:42 PM
#2
Re: std::map proper use?
Yes, it's standard behavior, operator[] of std::map inserts elements if they don't exist, it's intentional:
http://www.cplusplus.com/reference/s...tor%5B%5D/
If you merely want to check for existence, use the "find" member-function:
http://www.cplusplus.com/reference/stl/map/find/
or, alternatively, "count":
http://www.cplusplus.com/reference/stl/map/count/
Note that for std::multimap using "find" would be preferred (since std::multimap allows for duplicate elements its "count" mem.-fn. would have more work to do), but for std::map (which doesn't) I doubt the choice matters.
Last edited by mattpd; January 3rd, 2012 at 06:52 PM.
-
January 3rd, 2012, 07:08 PM
#3
Re: std::map proper use?
Cool, thanks for the reply.
-
January 4th, 2012, 02:22 AM
#4
Re: std::map proper use?
In "theory" the "best" method is using a "lower_bound", and checking the returned iterator. The advantage to this is that if it does not find your element, it still returns an "hint" for a more efficient future insertion.
That said, this should be reserved for the "optimization domain". IMO, i've always used operator[]. I try to prefer code that talks to me.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
January 4th, 2012, 06:22 AM
#5
Re: std::map proper use?
 Originally Posted by bkelly
My problem is that I realized that I was not explicitly adding entry [parameter_number] to the map. The code just jumped up and starting putting data in that entry as though it already exists. I am finding that rather worrying.
Since the map is defined as a map of structures and not defined as a map of pointers, does the simple act of referencing entry [1] (for example) create that entry. If so, then is it OK for the code to proceed to set various fields of that entry.
Whether this is ok depends on whether it is ok to overwrite an existing entry in the map. If it is, then the code is fine. It it's not, but the choice of your key should prevent this, then make sure you add an assertion that checks that the key is not present before using the operator []. If it's possible that the key is already present in the map and it should not be overwritten, then you'll have to write more verbose code using a combination of find/lower_bound and insert.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
January 5th, 2012, 04:35 AM
#6
Re: std::map proper use?
Just a minor style point, but you could make your code a little easier to read by using references rather than pointers.
Code:
td_parameter_map &parm_map = *p_msg_data->parameter_map;
parm_map[parameter_number].name = cs_t1;
This is especially relevent if the pointer is not expected to be changed.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|