Topic: Suggestions for new STL functions


Author: nesotto@cs.auc.dk ("Thorsten Ottosen")
Date: Thu, 15 Apr 2004 15:15:34 +0000 (UTC)
Raw View
Hi Joe,

FWIW, I think your suggestions are reasonable.

br

Thorsten


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jgottman@carolina.rr.com ("Joe Gottman")
Date: Wed, 14 Apr 2004 03:13:28 +0000 (UTC)
Raw View
   I have a couple of suggestions for new member functions to be added to
STL containers.  They would both be very easy to implement, and I think they
would be quite useful.  Below are rough specs for the new functions; I am
ignoring the other template parameters for simplicity here.

   1) T * vector<T>::data();
       T * const vector<T>::data() const;

      This function would return a pointer to the data stored by the vector
(which is guaranteed to be contiguous).  It is currently possible to get
this information from a vector v by typing
        &v[0]
but this invokes undefined behavior if the vector is empty.  The standard
string and the array template in the upcoming library TR already contain
this functionality.

   2)  Value & map<Key, Value>::at(Key const &key);
        Value & const map<Key, Value>::at(Key const &key) const;

   If the map contains an element whose Key is equivalent to key, at() would
return the element's Value part, just like operator[]().  However, if the
map does not contain such an element, at() would throw an exception
(probably a range_error).  There are many situations where it might be more
convenient to use at() rather than operator[]()
          A)  When Value is not default-constructible
          B)  When the map is const
          C) When you are accessing an element of the map as an rvalue and
you do not want to accidentally add an element
          D) When you know all the elements that should be in the map and
any attempt to access an element that is not in the map is an error

Joe Gottman


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]