Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::erase,std::erase_if(std::inplace_vector)

      From cppreference.com
      <cpp‎ |container‎ |inplace vector

      [edit template]
       
       
       
      std::inplace_vector
      Member types
      Member functions
      Non-member functions
      erase(std::inplace_vector)erase_if(std::inplace_vector)
       
      Defined in header<inplace_vector>
      template<class T,std::size_t N,class U= T>

      constexprtypenamestd::inplace_vector<T, N>::size_type

          erase(std::inplace_vector<T, N>& c,const U& value);
      (1)(since C++26)
      template<class T,std::size_t N,class Pred>

      constexprtypenamestd::inplace_vector<T, N>::size_type

          erase_if(std::inplace_vector<T, N>& c, Pred pred);
      (2)(since C++26)
      1) Erases all elements that compare equal tovalue from the containerc. Equivalent toauto it= std::remove(c.begin(), c.end(), value);
      auto r=std::distance(it, c.end());
      c.erase(it, c.end());
      return r;
      .
      2) Erases all elements that satisfy the predicatepred from the containerc. Equivalent toauto it=std::remove_if(c.begin(), c.end(), pred);
      auto r=std::distance(it, c.end());
      c.erase(it, c.end());
      return r;
      .

      Contents

      [edit]Parameters

      c - container from which to erase
      value - value to be removed
      pred - unary predicate which returns ​true if the element should be erased.

      The expressionpred(v) must be convertible tobool for every argumentv of type (possibly const)T, regardless ofvalue category, and must not modifyv. Thus, a parameter type ofT&is not allowed, nor isT unless forT a move is equivalent to a copy(since C++11).​

      [edit]Return value

      The number of erased elements.

      [edit]Complexity

      Linear.

      [edit]Example

      Run this code
      #include <cassert>#include <complex>#include <inplace_vector>#include <numeric>#include <print> int main(){std::inplace_vector<int,10> v(10,0);    std::ranges::iota(v,0);std::println("Initially, v = {}", v); auto erased=std::erase(v,3);std::println("After erase(v, 3), v = {}", v);assert(erased==1);     erased=std::erase_if(v,[](int x){return x%2==0;});std::println("After erasing all even numbers, v = {}", v);std::println("Erased even numbers: {}", erased); std::inplace_vector<std::complex<double>> nums{{2,2},{4,2},{4,8},{4,2}};std::erase(nums,{4,2});// since U = T, the value type can be ommitedstd::println("After erase {4, 2}, nums = {}", nums);}

      Output:

      Initially, v = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]After erase(v, 3), v = [0, 1, 2, 4, 5, 6, 7, 8, 9]After erasing all even numbers, v = [1, 5, 7, 9]Erased even numbers: 5After erase {4, 2}, nums = [(2,2), (4,8)]

      [edit]See also

      removes elements satisfying specific criteria
      (function template)[edit]
      removes elements satisfying specific criteria
      (algorithm function object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/inplace_vector/erase2&oldid=175548"

      [8]ページ先頭

      ©2009-2025 Movatter.jp