[C++-sig] export std::set
François Duranleau
duranlef at iro.umontreal.ca
Thu May 4 18:26:41 CEST 2006
On Wed, 3 May 2006, Andreas Beyer wrote:
> Here is the complete code:
If I may make a few suggestions:
> // element access
> bool contains(const KeyType key)
> { return count(key)>0; }
> // we must define add() for it gets explicit argument types
> void add(const KeyType key)
> { insert(key); }
> void remove(const KeyType key)
> // improve error handling here
> { if (!contains(key)) throw "element not in set"; erase(key); }
Why not put the key parameter as a const reference instead of copying?
Also, for remove, it would probably be best to write it like this:
void remove( const KeyType& key )
{
typename source_type::iterator i = this->find( key ) ;
if ( i == this->end() )
{
throw "element not in set" ;
}
this->erase( i ) ;
}
This way you look up for the key only once, not twice.
Also, everywhere in py_set<>, you need to either make all calls to some
base class's (with template arguments dependent on the current class)
method qualified (e.g. with this-> as above) or "import" them with 'using'
declarations, or else this code is not standard and won't compile on some
compilers (the one I use, g++-4.0, for instance).
--
François Duranleau
LIGUM, Université de Montréal
"The real source of wealth is correct ideas: workable ideas: that is,
negative entropy -- Information."
- Robert Anton Wilson, _Prometheus Rising_, 1983
More information about the Cplusplus-sig
mailing list