Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::sub_match<BidirIt>::swap

      From cppreference.com
      <cpp‎ |regex‎ |sub match
       
       
       
      Regular expressions library
      Classes
      (C++11)
      Algorithms
      Iterators
      Exceptions
      Traits
      Constants
      (C++11)
      Regex Grammar
       
       
      void swap( sub_match& s)noexcept(/* see below */);
      (since C++11)

      Exchanges the contents of two sub-match objects. Equivalent to

      this->pair<BidirIt, BidirIt>::swap(s);
      std::swap(matched, s.matched);

      Contents

      [edit]Parameters

      s - asub_match to swap with
      Type requirements
      -
      BidirIt must meet the requirements ofLegacySwappable.

      [edit]Exceptions

      noexcept specification:  
      noexcept(std::is_nothrow_swappable_v<BidirIt>)

      [edit]Example

      Run this code
      #include <cassert>#include <iostream>#include <regex> int main(){constchar* s="Quick red cat";std::sub_match<constchar*> x, y;     x.first=&s[0];    x.second=&s[5];    x.matched=false;     y.first=&s[012];    y.second=&s[13];    y.matched=true; std::cout<<"Before swap:\n";std::cout<<"x.str() = ["<< x.str()<<"]\n";std::cout<<"y.str() = ["<< y.str()<<"]\n";assert(!x.matched and y.matched);     x.swap(y); std::cout<<"After swap:\n";std::cout<<"x.str() = ["<< x.str()<<"]\n";std::cout<<"y.str() = ["<< y.str()<<"]\n";assert(x.matched and!y.matched);}

      Output:

      Before swap:x.str() = []y.str() = [cat]After swap:x.str() = [cat]y.str() = []

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3204C++11std::sub_match used inheritedstd::pair::swap(pair&)
      which led to a slicing
      std::sub_match::swap(sub_match&) is added
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/regex/sub_match/swap&oldid=178372"

      [8]ページ先頭

      ©2009-2025 Movatter.jp