std::end
std::end (범위 끝 반복자)
주어진 범위·컨테이너·배열의 끝(past-the-end)을 가리키는 반복자를 돌려주는 함수예요. C++11부터 있어요.
출처: cppreference
본문
다양한 컨테이너 헤더와 <iterator> 헤더에 정의돼 있어요.
// (1) C++11 (C++17부터 constexpr)
template< class C >
auto end( C& c ) -> decltype(c.end());
// (2) C++11 (C++17부터 constexpr)
template< class C >
auto end( const C& c ) -> decltype(c.end());
// (3) C++11 (C++14부터 noexcept, constexpr)
template< class T, std::size_t N >
T* end( T (&array)[N] );
// (4) C++11, cend
template< class C >
constexpr auto cend( const C& c ) noexcept(/* ... */) -> decltype(std::end(c));
- 1,2) 컨테이너
c의end()를 반환해요. -
- 배열의 마지막 요소 뒤를 가리키는 포인터를 돌려줘요.
cend는const끝 반복자를 돌려줘요.
반환값
범위의 마지막 요소 다음을 가리키는 반복자·포인터예요.
예제
#include <iostream>
#include <vector>
#include <iterator>
int main()
{
std::vector<int> v = {3, 1, 4};
int a[3] = {1, 2, 3};
// std::distance(std::begin(v), std::end(v)) 등으로 사용
std::cout << (std::end(a) - std::begin(a)) << '\n'; // "3"
}