×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

emptying std::map

emptying std::map

emptying std::map

(OP)
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)
 

RE: emptying std::map

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);
}
 

RE: emptying std::map

(OP)
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)
 

RE: emptying std::map

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.
 

RE: emptying std::map

(OP)

Quote:

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)
 

RE: emptying std::map

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;)...
 

RE: emptying std::map

(OP)
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)
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close