В C ++ 0x / 11 мы получаем std::begin
и std::end
, которые перегружены для массивов:
#include <algorithm>
int main(){
int v[2000];
std::sort(std::begin(v), std::end(v));
}
Если вы неу меня нет доступа к C ++ 0x, написать их несложно:
// for container with nested typedefs, non-const version
template<class Cont>
typename Cont::iterator begin(Cont& c){
return c.begin();
}
template<class Cont>
typename Cont::iterator end(Cont& c){
return c.end();
}
// const version
template<class Cont>
typename Cont::const_iterator begin(Cont const& c){
return c.begin();
}
template<class Cont>
typename Cont::const_iterator end(Cont const& c){
return c.end();
}
// overloads for C style arrays
template<class T, std::size_t N>
T* begin(T (&arr)[N]){
return &arr[0];
}
template<class T, std::size_t N>
T* end(T (&arr)[N]){
return arr + N;
}