public member function
<regex>

std::match_results::position

difference_type position (size_type n = 0) const;
返回匹配的位置
返回目標序列中第 *n* 個匹配項的第一個字元在match_results物件中的位置,該物件是ready狀態。

這是目標序列中的第一個字元與匹配的第一個字元之間的距離。

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

引數

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

返回值

序列中匹配的位置。

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::position
// * using smatch, a standard alias of match_results<string::iterator>
#include <iostream>
#include <string>
#include <regex>

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

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

  for (unsigned i=0; i<m.size(); ++i) {
    std::cout << "match " << i << " (" << m[i] << ") ";
    std::cout << "at position " << m.position(i) << std::endl;
  }

  return 0;
}

輸出
match 0 (subject) at position 5
match 1 (sub) at position 5
match 2 (ject) at position 8


另見