public member function
<regex>

std::match_results::length

difference_type length (size_type n = 0) const;
返回匹配的長度
返回在match_results物件中第n個匹配的長度(以字元為單位),該物件是ready

它返回與其第nsub_match元素的length成員相同的值。

match_results物件應該是ready的,這發生在它在呼叫regex_matchregex_search時作為合適的引數傳遞之後。

引數

n
匹配編號。 此編號應低於match_results::size
匹配編號0表示整個匹配的表示式。 如果有任何子表示式,則後續的匹配編號標識這些子表示式。
成員型別size_type是一種無符號整型型別。

返回值

子匹配n中的字元數。

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

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// match_results::length
// * using smatch, a standard alias of match_results<string::iterator>
#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.length(i) << 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


另見