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

履歴 編集

function
<regex>

std::match_results::コンストラクタ(C++11)

match_results(const Allocator& a = Allocator());    // (1)
match_results() : match_results(Allocator()) {}     // (1) C++20

match_results(const Allocator& a);                  // (2) C++20

match_results(const match_results& m);              // (3)

match_results(match_results&& m) noexcept;          // (4)

概要

match_results オブジェクトを構築する。

要件

  • (4) Allocator のムーブコンストラクタは例外で終了しないこと。

効果

  • (1) デフォルトコンストラクタ。
    • C++17まで : 指定したアロケータaを用いてmatch_results オブジェクトを構築する。
    • C++20 : アロケータをデフォルト構築してmatch_results オブジェクトを構築する。
  • (2) 指定したアロケータaを用いてmatch_results オブジェクトを構築する。
  • (3) コピーコンストラクタ。引数 m をコピーした match_results オブジェクトを構築する。
  • (4) ムーブコンストラクタ。引数 m をムーブした match_results オブジェクトを構築する。

事後条件

計算量

  • (1)(2) 定数時間
  • (3) 線形時間
  • (4) 定数時間

備考

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

#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;                   // (1) の形式
  print(m1);

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

  std::cmatch m2(m1);               // (3) の形式
  print(m2);

  std::cmatch m3(std::move(m1));    // (4) の形式
  print(m3);
}

出力

ready:false

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 を間接参照した結果から (2)、あるいは、(3) の形式で構築した場合に position の結果が正しくコピーされない。これは、4.9.3 以降で修正される予定である。

参照