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

履歴 編集

function template
<regex>

std::swap (非メンバ関数)(C++11)

template <class charT, class traits>
void swap(basic_regex<charT, traits>& lhs, basic_regex<charT, traits>& rhs);

概要

2 つの正規表現オブジェクトを交換する。

効果

lhs.swap(rhs)

戻り値

なし

#include <iostream>
#include <regex>

void print(const std::regex& re)
{
  const char s[] = " abc 123 def ";
  std::cmatch m;
  std::regex_search(s, m, re);
  std::cout << "ready:" << std::boolalpha << m.ready() << std::endl;
  if (m.ready()) {
    std::cout << "prefix:'" << m.prefix() << '\'' << std::endl;
    for (std::size_t i = 0, n = m.size(); i < n; ++i) {
      std::cout << i << ":'" << m.str(i) << "', position = " << m.position(i) << std::endl;
    }
    std::cout << "suffix:'" << m.suffix() << '\'' << std::endl;
  }
  std::cout << std::endl;
}

int main()
{
  std::regex re1("\\w+");
  std::regex re2("\\d+");

  // swap 前
  print(re1);
  print(re2);

  std::swap(re1, re2);

  // swap 後
  print(re1);
  print(re2);
}

出力

ready:true
prefix:' '
0:'abc', position = 1
suffix:' 123 def '

ready:true
prefix:' abc '
0:'123', position = 5
suffix:' def '

ready:true
prefix:' abc '
0:'123', position = 5
suffix:' def '

ready:true
prefix:' '
0:'abc', position = 1
suffix:' 123 def '

バージョン

言語

  • C++11

処理系