Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c++; map interator; detecting when a find() returns an invalid iterator

Posted on 2008-10-14
4
Medium Priority
?
1,091 Views
Last Modified: 2012-05-05
I've got a map<string,string> as part of a class and am writing a function to delete one of the map items, based on the key name. Once I obtain the iterator - through a find() - I tell it to erase it from the map.  Works great if it finds a match.  If it doesn't find a match, VS2005 pops up an errors "debug assertion failed!".  

I've looked online and cannot find anything which dictates how to test an iterator for a NULL/invalid value.  I finally discovered a solution which IMHO seems rather clunky... but it does the job.

if(iter._Ptr->_Isnil) { return; } //  exit method if invalid iterator

I find it difficult to believe this is the proper way to test for an invalid iterator.  Is there a better way?  The full 'delete' method is listed below.

Oh and I suppose I should mention, this is a cross-platform app, so g++ 4.1.2 will also need to be able to compile it.
void deleteItem(string key) {
    map<string,string>::iterator i = this->_Items->find(key);
    if(i._Ptr->_Isnil) { return; }
    this->_Items->erase(i);
  }

Open in new window

0
Comment
Question by:BaconU
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
LordOfPorts earned 300 total points
ID: 22713200
If find does not detect value it should return map::end http://www.cplusplus.com/reference/stl/map/find.html
0
 
LVL 3

Expert Comment

by:wktang83
ID: 22713289

void deleteItem(string key) {
    map<string,string>::iterator i = this->_Items->find(key);
    if(i._Ptr == this->_Items->end()) { return; }
    this->_Items->erase(i);
  }

Open in new window

0
 

Author Closing Comment

by:BaconU
ID: 31505968
Ah, yes... I should have known.  I mistakenly assumed that the end() was only to be used in conjunction with begin().  

This is a much nicer solution!  Thanks LordOfPorts!
0
 

Author Comment

by:BaconU
ID: 22713868
wktang83, according to my tests, you don't need the ._Ptr when testing again end().
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question