size_ の範囲外チェックをせずに、heap_[0] と heap_[size_-1] にそれぞれアクセスします。もし size_ が 0 の場合は、不定動作となります。
template <typename T>
class myvector
{
...
/**
* @brief Returns a reference to the first element in the container.
* Calling front on an empty container is undefined.
* @return Reference to the first element
*/
reference front(void)
{
return heap_[0];
}
/**
* @brief Returns a reference to the first element in the container.
* Calling front on an empty container is undefined.
* @return Const reference to the first element
*/
const_reference front(void) const
{
return heap_[0];
}
/**
* @brief Returns reference to the last element in the container.
* Calling back on an empty container is undefined.
* @return Reference to the last element.
*/
reference back(void)
{
return heap_[size_ - 1];
}
/**
* @brief Returns reference to the last element in the container.
* Calling back on an empty container is undefined.
* @return Const reference to the last element.
*/
const_reference back(void) const
{
return heap_[size_ - 1];
}
...
};
front() と back() はそれぞれ2種類ありますが、違いは const関数であるか、非const関数であるか、のみです。
全ソースコード: https://github.com/suomesta/myvector/tree/master/021
0 件のコメント:
コメントを投稿