이 기사에서는 C++ STL에서 match_results 연산자 '[ ]'의 작동, 구문 및 예에 대해 논의할 것입니다.
C++ STL의 match_results란 무엇입니까?
std::match_results는 일치하는 문자 시퀀스 컬렉션을 보유하는 데 사용되는 특수 컨테이너 유사 클래스입니다. 이 컨테이너 클래스에서 정규식 일치 작업은 대상 시퀀스의 일치 항목을 찾습니다.
match_results 연산자 '[ ]'란 무엇입니까
Match_results 연산자 []는 match_results의 i번째 위치를 직접 참조하는 데 사용되는 참조 연산자입니다. 연산자 []는 연결된 개체의 i번째 일치 위치를 반환합니다. 이 연산자는 0부터 시작하는 일치 위치로 요소에 직접 액세스해야 할 때 유용합니다.
구문
match_results1[int i];
매개변수
이 연산자는 액세스하려는 요소의 정수 유형의 매개변수 1개를 사용합니다.
반환 값
이 함수는 일치 결과의 i번째 위치에 대한 참조를 반환합니다.
예시
Input: string str = "TutorialsPoint";
regex R("(Tutorials)(.*)");
smatch Mat;
regex_match(str, Mat, R);
Mat[0];
Output: TutorialsPoint 예시
#include <bits/stdc++.h>
using namespace std;
int main() {
string str = "TutorialsPoint";
regex R("(Tutorials)(.*)");
smatch Mat;
regex_match(str, Mat, R);
for (int i = 0; i < Mat.size(); i++) {
cout<<"Match is : " << Mat[i]<< endl;
}
return 0;
} 출력
위의 코드를 실행하면 다음 출력이 생성됩니다 -
Match is : TutorialsPoint Match is : Tutorials Match is : Point
예시
#include <bits/stdc++.h>
using namespace std;
int main() {
string str = "Tutorials Point";
regex R("(Tutorials)(Point)(.*)");
smatch Mat;
regex_match(str, Mat, R);
int len = 0;
string S;
for(int i = 1; i < Mat.size(); i++) {
if (Mat.length(i) > len) {
str = Mat[i];
len = Mat.length(i);
}
}
cout<<"Matching length is : " << len<< endl;
return 0;
} 출력
위의 코드를 실행하면 다음 출력이 생성됩니다 -
Matching length is : 0