ranges::find
ranges::find (범위에서 원소 찾기)
범위에서 특정 값 또는 조건을 만족하는 첫 원소를 찾는 ranges 버전 알고리즘이에요. <algorithm> 헤더에 있어요.
출처: cppreference
본문
std::ranges::find는 범위에서 value와 같은 첫 원소를, find_if는 술어가 참인 첫 원소를, find_if_not은 술어가 거짓인 첫 원소를 찾아요.
namespace std::ranges {
template< std::input_iterator I, std::sentinel_for<I> S,
class T, class Proj = std::identity >
constexpr I find( I first, S last, const T& value, Proj proj = {} );
}
- 반환 값: 찾은 첫 원소를 가리키는 반복자. 없으면
last. ranges::find는operator==로,ranges::find_if/find_if_not은 술어로 판정해요.proj로 요소를 투영해 비교할 수 있어요.
std::vector<int> v{1, 4, 7, 2};
auto it = std::ranges::find(v, 7); // 7의 위치
// or
auto it2 = std::ranges::find_if(v, [](int x){ return x > 5; });
범위 하나로 간결하게 "처음 나오는 조건 일치 원소"를 찾는 기본 함수예요.