最終更新日時(UTC):
が更新

履歴 編集

function
<list>

std::list::clear

void clear();                    // (1) C++03
void clear() noexcept;           // (1) C++11
constexpr void clear() noexcept; // (1) C++26

概要

全ての要素を削除する

効果

listオブジェクトが管理しているすべての要素を破棄する。 また、要素を指す全ての参照、ポインタ、イテレータが無効になる。past-the-end イテレータは無効にならない。

戻り値

なし

例外

投げない

計算量

線形時間。全ての要素に対してデストラクタを呼び出す。

#include <iostream>
#include <cassert>
#include <list>

int main()
{
  std::list<int> ls = {1, 2, 3};

  ls.clear();

  assert(ls.empty());

  for (int x : ls) {
    std::cout << x << std::endl;
  }
}

出力

バージョン

言語

  • C++11

処理系

参照