find_end

find_end (부분 시퀀스의 마지막 위치 찾기)

첫 범위 안에서 두 번째 범위(부분 시퀀스)와 일치하는 부분의 마지막 시작 위치를 찾는 알고리즘이에요. <algorithm> 헤더에 있어요.

출처: cppreference

본문

find_end[first1, last1) 안에서 [first2, last2)와 일치하는(부분 문자열 같은) 마지막 위치를 찾아요.

template< class ForwardIt1, class ForwardIt2 >
ForwardIt1 find_end( ForwardIt1 first1, ForwardIt1 last1,
                     ForwardIt2 first2, ForwardIt2 last2 );   // (1)

비교기 p를 받는 버전도 있어요.

template< class ForwardIt1, class ForwardIt2, class BinaryPred >
ForwardIt1 find_end( ForwardIt1 first1, ForwardIt1 last1,
                     ForwardIt2 first2, ForwardIt2 last2,
                     BinaryPred p );   // (2)
    1. operator==로, 2) 술어 p로 원소를 비교해요.
  • 반환 값: 일치하는 마지막 부분 시퀀스의 시작을 가리키는 반복자. 없거나 [first2, last2)가 비어 있으면 last1.

std::search가 "첫 번째" 일치를 찾는 데 비해, find_end는 "마지막" 일치를 찾아요.

std::string s = "ababcab";
std::string pat = "ab";
auto it = std::find_end(s.begin(), s.end(), pat.begin(), pat.end());
// 마지막 "ab"의 시작 위치

문자열이나 컨테이너에서 어떤 부분 시퀀스가 마지막으로 등장하는 위치가 필요할 때 유용해요.

더 알아보기 (Learn more)

cppreference