public member function
<regex>

std::sub_match::length

difference_type length() const;
返回長度
返回子匹配項的長度(以字元為單位)。

這是物件作為資料(成員firstsecond)儲存的迭代器之間的距離,如果物件狀態是matched。 否則,它是0.

引數



返回值

子匹配項中的字元數。

difference_type是一個成員型別,定義為迭代器型別使用的差分型別的別名BidirectionalIterator(模板引數)。 這通常是一個帶符號的整數型別。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// sub_match::length
#include <iostream>
#include <string>
#include <regex>

int main ()
{
  std::string s ("subject");
  std::smatch m;
  std::regex e ("(sub)(.*)");

  std::regex_match ( s, m, e );

  for (unsigned i=0; i<m.size(); ++i) {
    std::cout << "match " << i << " (" << m[i] << ")";
    std::cout << " has a length of " << m[i].length() << std::endl;
  }
  return 0;
}

輸出
match 0 (subject) has a length of 7
match 1 (sub) has a length of 3
match 2 (ject) has a length of 4


另見