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

履歴 編集

function
<deque>

std::deque::clear

void clear();           // C++03
void clear() noexcept;  // C++11

概要

全ての要素を削除する。

事後条件

empty() == true

戻り値

なし

例外

投げない

計算量

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

#include <iostream>
#include <cassert>
#include <deque>
#include <algorithm>

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

  c.clear();

  assert(c.empty());

  std::for_each(c.begin(), c.end(), [](int x) {
    std::cout << x << std::endl;
  });
}

出力

参照

  • LWG Issue 2231. DR 704 removes complexity guarantee for clear()
    • C++03までこの関数の効果はerase(begin(), end())だったため、それによって線形時間の計算量が保証されていたが、C++11で効果の表記が変わったために、保証がなくなってしまっていた。C++14であらためて保証を追加。

関連項目

名前 説明
erase 指定した要素を削除する
resize 要素数を変更する
pop_back 末尾要素を削除する
pop_front 先頭要素を削除する
empty コンテナが空であるかどうかを調べる