Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

emptying std::map

Status
Not open for further replies.

bkelly13

Programmer
Aug 31, 2006
98
US
I have an std::map that is working, but there are problems when I try to empty it. The declaration looks like this:
Code:
std::map< int, DP_MESSAGE_STRUCT * > m_dp_messages;
std::map< int, DP_MESSAGE_STRUCT * >::iterator  m_msg_iterator;
I can put data in the map and read the data. The structure has an array that is created dynamically. The code to delete the arrays looks something like this. (I write something because I cannot cut and paste so please excuse typos.)
Code:
m_msg_iterator = m_dp_messages.begin();
bool empty = m_dp_messages.empty();  // false when I get a problem
while( m_msg_iterator != m_dp_messages.end() )
{
   temp = m_msg_iterator->second;
   delete[] temp->buffer;  // the structure contains an array pointer.
   m_dp_messages.erase( m_msg_iterator);
   m_msg_iterator = m_dp_messages.begin();
}
Is this the correct way to remove all the entries from a std::map?




We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 
Right way to clear the map:

Code:
m_msg_iterator = m_dp_messages.begin();
while (m_msg_iterator != m_dp_messages.end()) {
   DP_MESSAGE_STRUCT* temp = m_msg_iterator->second;
   delete[] temp->buffer;
   // Where is the structure object *temp?
   // It lives out of the map...
   m_msg_iterator = m_dp_messages.erase(m_msg_iterator);
}
 
That looks better than what I had.
Follow up: I have been accessing this map like an array:
Code:
m_dp_messages[ x ] p_struct;
p_struct = m_dp_messages[ x ];

While that is much simpler that using the iterator, it occurs to me that I cannot detect when there is no entry for key x. With this example, I can tell if the pointer is null. But another map I use takes the format:
Code:
std::map< CString, int> name_to_id;
In this case I presume that I must use an iterator to determine if the entry is not present in the map. Is that the case?

Thank you for taking the time to reply.

We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 
The map indexing operator INSERTS a new element into the map if no such key in the map, so it's impossible to detect no entry condition with this operarator.

Use find member:
Find before use:
Code:
if ((it = m.find(key)) != m.end()) { // entry OK
   ... use iterator or [] accessor
} else { // no entry
   ...
}
or w/o iterator:
Code:
if (m.find(key)) != m.end()) { // entry OK
   ... use [] accessor on existing entry
} else { // no entry
   ...
}

Don't use pointers to detect no entry condition only.
 
The map indexing operator INSERTS a new element into the map if no such key in the map...

Is this true even when used as an rvalue? As in:
Code:
 int = my_map[ key ];

If the entry for key does not exist, how could it make one as the value is unknown?

We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 
Yes. It is initialized by default constructor (value).
Try this
Code:
std::map<int,int> m;
int x = 666;
x = m[0];
std::cout << x << std::endl;
and see what happens (one of the best way to get the instant answer to this kind of questions;)...
 
Cool. You set me straight on a couple of points and I have the maps working well.
Thank you,

We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top