Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::erase_if(std::flat_set)

      From cppreference.com
      <cpp‎ |container‎ |flat set

      [edit template]
       
       
       
       
      Defined in header<flat_set>
      template<class Key,class Compare,class KeyContainer,

               class Pred>
      std::flat_set<Key, Compare, KeyContainer>::size_type
          erase_if(std::flat_set<Key, Compare, KeyContainer>& c,

                    Pred pred);
      (since C++23)
      (constexpr since C++26)

      Erases all elements that satisfy the predicatepred fromc.

      The predicatepred is satisfied if the expressionbool(pred(std::as_const(e))) istrue, wheree is some element inc.

      IfKey is notMoveAssignable, the behavior is undefined.

      Contents

      [edit]Parameters

      c - container adaptor from which to erase
      pred - predicate that returnstrue if the element should be erased

      [edit]Return value

      The number of erased elements.

      [edit]Complexity

      Exactlyc.size() applications of the predicatepred.

      Exceptions

      Iferase_if throws,c remains in valid but unspecified (maybe empty) state.

      Notes

      The algorithm is stable, that is, the order of elements that are not deleted remains unchanged.

      [edit]Example

      Run this code
      #include <iostream>#include <flat_set> void println(auto rem,constauto& container){std::cout<< rem<<'{';for(char sep[]{0,' ',0};constauto& item: container)std::cout<< sep<< item,*sep=',';std::cout<<"}\n";} int main(){std::flat_set data{3,3,4,5,5,6,6,7,2,1,0};    println("Original:\n", data); auto divisible_by_3=[](constauto& x){return(x%3)==0;}; constauto count=std::erase_if(data, divisible_by_3);     println("Erase all items divisible by 3:\n", data);std::cout<< count<<" items erased.\n";}

      Output:

      Original:{0, 1, 2, 3, 4, 5, 6, 7}Erase all items divisible by 3:{1, 2, 4, 5, 7}3 items erased.

      [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/flat_set/erase_if&oldid=168798"

      [8]ページ先頭

      ©2009-2025 Movatter.jp