最終更新日時:
が更新

履歴 編集

function
<regex>

std::sub_match::swap(C++23)

void swap(sub_match& s) noexcept(see below); // C++23

概要

別のsub_matchオブジェクトとデータを交換する。

事前条件

BidirectionalIteratorSwappableの要件を満たすこと。

効果

以下と等価:

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

例外

この関数の例外仕様は、is_nothrow_swappable_v<BidirectionalIterator>と等価である。

備考

  • このメンバ関数はC++23に対する欠陥報告 (LWG 3204) として追加されたものであり、コンパイラは早期に対応している場合がある。そのため、C++20モードでも使用できる可能性がある。

#include <iostream>
#include <regex>
#include <string>

int main()
{
  const std::string s(" abc 123 ");
  const std::regex re(R"((\w+)\s+(\d+))");
  std::smatch m;
  std::regex_search(s, m, re);

  std::ssub_match a = m[1]; // "abc"
  std::ssub_match b = m[2]; // "123"

  a.swap(b);

  std::cout << a.str() << std::endl;
  std::cout << b.str() << std::endl;
}

出力

123
abc

バージョン

言語

  • C++23

処理系

関連項目

参照