023: empty()

empty() を定義します。

サイズ 0 なら true を返します。

template <typename T>
class myvector
{
...

    /**
     * @brief      Checks if the container has no elements.
     * @return     true if the container is empty, false otherwise.
     */
    bool empty(void) const noexcept
    {
        return size_ == 0;
    }

    ...
};
ジェネリックな empty() の実装として、return begin() == end(); が知られています。
myvectorの場合は end() は begin() と size_ から作るので、return size_ == 0; としました。


全ソースコード: https://github.com/suomesta/myvector/tree/master/023

0 件のコメント:

コメントを投稿