Wednesday, October 26, 2016

STL iterator [04] : std::begin() std::end() std::next() ---returns an iterator to the ** of the given container C or array array.

[References]
1. "std::begin, from enseignement.polytechnique.fr...."
2. "std::end, same above"
3. "std::next, from same above"


std::begin

Defined in header <iterator>
template< class C >
auto begin( C& c ) -> decltype(c.begin());
(1)(since C++11)
template< class C >
auto begin( const C& c ) -> decltype(c.begin());
(1)(since C++11)
(2)
template< class T, size_t N >
T* begin( T (&array)[N] );
(since C++11)
(until C++14)
template< class T, size_t N >
constexpr T* begin( T (&array)[N] );
(since C++14)
template< class C >
constexpr auto cbegin( const C& c ) -> decltype(std::begin(c));
(3)(since C++14)
Returns an iterator to the beginning of the given container c or array array.
1) Returns a possibly const-qualified iterator to the beginning of the container c.
2) Returns a pointer to the beginning of the array array.
3) Returns a const-qualified iterator to the beginning of the container c.
range-begin-end.svg

Parameters

c-a container with a begin method
array-an array of arbitrary type

Return value

An iterator to the beginning of c or array

Exceptions

2) 
noexcept specification:  
noexcept
  
 (since C++14)
3) 
noexcept specification:  
noexcept(noexcept(std::begin(c)))

Notes

In addition to being included in <iterator>std::begin is guaranteed to become available if any of the following headers are included: <array><deque><forward_list><list><map><regex><set><string><unordered_map>,<unordered_set>, and <vector>.

User-defined overloads

Custom overloads of std::begin may be provided for classes that do not expose a suitable begin() member function, yet can be iterated. The following overloads are already provided by the standard library:
specializes std::begin
(function template)
specializes std::begin
(function template)
Similar to the use of swap (described in Swappable), typical use of the begin function in generic context is an equivalent of using std::begin; begin(arg);, which allows both the ADL-selected overloads for user-defined types and the standard library function templates to appear in the same overload set.
template<typename Container, typename Function>
void for_each(Container&& cont, Function f) {
    using std::begin;
    auto it = begin(cont);
    using std::end;
    auto end_it = end(cont);
    while (it != end_it) {
        f(*it);
        ++it;
    }
}

Example

#include <iostream>
#include <vector>
#include <iterator>
 
int main() 
{
    std::vector<int> v = { 3, 1, 4 };
    auto vi = std::begin(v);
    std::cout << *vi << '\n'; 
 
    int a[] = { -5, 10, 15 };
    auto ai = std::begin(a);
    std::cout << *ai << '\n';
}
Output:
3
-5

std::end

Defined in header <iterator>
template< class C >
auto end( C& c ) -> decltype(c.end());
(1)(since C++11)
template< class C >
auto end( const C& c ) -> decltype(c.end());
(1)(since C++11)
(2)
template< class T, size_t N >
T* end( T (&array)[N] );
(since C++11)
(until C++14)
template< class T, size_t N >
constexpr T* end( T (&array)[N] );
(since C++14)
template< class C >
constexpr auto cend( const C& c ) -> decltype(std::end(c));
(3)(since C++14)
Returns an iterator to the end (i.e. the element after the last element) of the given container c or array array.
1) Returns a possibly const-qualified iterator to the end of the container c.
2) Returns a pointer to the end of the array array.
3) Returns a const-qualified iterator to the end of the container c.
range-begin-end.svg

Parameters

c-a container with an end method
array-an array of arbitrary type

Return value

An iterator to the end of c or array. Note that the end of a container or array is defined as the element following the last valid element.

Exceptions

2) 
noexcept specification:  
noexcept
  
 (since C++14)
3) 
noexcept specification:  
noexcept(noexcept(std::end(c)))

Notes

In addition to being included in <iterator>std::end is guaranteed to become available if any of the following headers are included: <array><deque><forward_list><list><map><regex><set><string><unordered_map>,<unordered_set>, and <vector>.

User-defined overloads

Custom overloads of std::end may be provided for classes that do not expose a suitable end() member function, yet can be iterated. The following overloads are already provided by the standard library:
specializes std::end
(function template)
specializes std::end
(function template)
Similar to the use of swap (described in Swappable), typical use of the end function in generic context is an equivalent of using std::end; end(arg);, which lets both the ADL-selected overloads for user-defined types and the standard library function templates to appear in the same overload set.
template<typename Container, typename Function>
void for_each(Container&& cont, Function f) {
    using std::begin;
    auto it = begin(cont);
    using std::end;
    auto end_it = end(cont);
    while (it != end_it) {
        f(*it);
        ++it;
    }
}

Example

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
 
int main() 
{
    std::vector<int> v = { 3, 1, 4 };
    if (std::find(std::begin(v), std::end(v), 5) != std::end(v)) {
        std::cout << "found a 5 in vector v!\n";
    }
 
    int a[] = { 5, 10, 15 };
    if (std::find(std::begin(a), std::end(a), 5) != std::end(a)) {
        std::cout << "found a 5 in array a!\n";
    }
}
Output:
found a 5 in array a!


std::next

Defined in header <iterator>
template< class ForwardIt >
ForwardIt next( ForwardIt it, 
                typename std::iterator_traits<ForwardIt>::difference_type n = 1 );
(since C++11)
Return the nth successor of iterator it.

Parameters

it-an iterator
n-number of elements to advance
Type requirements
-
 ForwardIt must meet the requirements of ForwardIterator.

Return value

The nth successor of iterator it.

Possible implementation

template<class ForwardIt>
ForwardIt next(ForwardIt it, typename std::iterator_traits<ForwardIt>::difference_type n = 1)
{
    std::advance(it, n);
    return it;
}

Notes

Although the expression ++c.begin() often compiles, it is not guaranteed to do so: c.begin() is an rvalue expression, and there is no BidirectionalIterator requirement that specifies that increment of an rvalue is guaranteed to work. In particular, when iterators are implemented as pointers, ++c.begin() does not compile, while std::next(c.begin()) does.

Example

#include <iostream>
#include <iterator>
#include <vector>
 
int main() 
{
    std::vector<int> v{ 3, 1, 4 };
 
    auto it = v.begin();
 
    auto nx = std::next(it, 2);
 
    std::cout << *it << ' ' << *nx << '\n';
}
Output:
3 4
..
...