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

履歴 編集

function
<regex>

std::match_results::operator=(C++11)

match_results& operator=(const match_results& m);       // (1)

match_results& operator=(match_results&& m) noexcept;   // (2)

概要

match_results オブジェクトを代入する。

要件

  • (1) value_typesub_match<BidirectionalIterator>)はこのコンテナに対してコピー挿入可能(CopyInsertable)であること。
  • (2) allocator_traits<allocator_type>::propagate_on_container_move_assignment::value == false である場合、value_typesub_match<BidirectionalIterator>)はこのコンテナに対してムーブ挿入可能(MoveInsertable)であること。

効果

  • (1) コピー代入演算子。引数 m*this にコピー代入する。
  • (2) ムーブ代入演算子。引数 m*this にムーブ代入する。*this の全ての既存の要素はムーブ代入されるか破棄される。

事後条件

計算量

  • (1) 線形時間
  • (2) 線形時間

備考

規格では明確ではないものの、以下の事後条件を満たすべきであると思われる。

#include <iostream>
#include <regex>

void print(const std::cmatch& m)
{
  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) << '\'' << std::endl;
    }
-    std::cout << "suffix:'" << m.suffix() << '\'' << std::endl;
  }
  std::cout << std::endl;
}

int main()
{
  const char s[] = " abc 123 def ";
  const std::regex re("(\\w+) (\\d+) (\\w+)");

  std::cmatch m1, m2, m3;

  std::regex_search(s, m1, re);
  print(m1);

  m2 = m1;                          // (1) の形式
  print(m2);

  m3 = std::move(m1);               // (2) の形式
  print(m3);
}

出力

ready:true
prefix:' '
0:'abc 123 def'
1:'abc'
2:'123'
3:'def'
suffix:' '

ready:true
prefix:' '
0:'abc 123 def'
1:'abc'
2:'123'
3:'def'
suffix:' '

ready:true
prefix:' '
0:'abc 123 def'
1:'abc'
2:'123'
3:'def'
suffix:' '

バージョン

言語

  • C++11

処理系

備考

GCC(libstdc++) の 4.9.2 までは、アロケータが上記の備考欄のようには設定されず、また、regex_iterator を間接参照した結果を代入した場合に position の結果が正しくコピーされない。これは、4.9.3 以降で修正される予定である。