 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Joe Gottman Guest
|
Posted: Wed Apr 14, 2004 3:13 am Post subject: Suggestions for new STL functions |
|
|
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 ]
|
|
| Back to top |
|
 |
Thorsten Ottosen Guest
|
Posted: Thu Apr 15, 2004 3:15 pm Post subject: Re: Suggestions for new STL functions |
|
|
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 ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|