Range access
在 Iterator library# Range access 总结了 range access interface,显然这样的设计是遵循"the interface principle"的,这样的设计能够充分发挥ADL。
cppreference Iterator library# Range access
interface | explanation |
---|---|
begin cbegin(C++11)(C++14) | returns an iterator to the beginning of a container or array (function template) |
end cend(C++11)(C++14) | returns an iterator to the end of a container or array (function template) |
rbegin crbegin(C++14) | returns a reverse iterator to a container or array (function template) |
rend crend(C++14) | returns a reverse end iterator for a container or array (function template) |
size ssize(C++17)(C++20) | returns the size of a container or array (function template) |
empty(C++17) | checks whether the container is empty (function template) |
data(C++17) | obtains the pointer to the underlying array (function template) |
Example
rosettacode Loop over multiple arrays simultaneously
#include <iostream>
#include <iterator>
int main(int argc, char *argv[])
{
char lowers[] = { 'a', 'b', 'c' };
char uppers[] = { 'A', 'B', 'C' };
int nums[] = { 1, 2, 3 };
auto ilow = std::begin(lowers);
auto iup = std::begin(uppers);
auto inum = std::begin(nums);
for (; ilow != std::end(lowers) and iup != std::end(uppers) and inum != std::end(nums); ++ilow, ++iup, ++inum)
{
std::cout << *ilow << *iup << *inum << "\n";
}
}
User-defined overloads and ADL
关于这一点,在cppreference std::begin, std::cbegin、cppreference std::end, std::cend中,都有描述,下面是截取自cppreference std::begin, std::cbegin:
Similar to the use of
swap
(described in Swappable), typical use of thebegin
function in generic context is an equivalent ofusing 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.