如何使用标准模板库 std::sort() 对声明为的数组进行排序int v[2000];
How to use standard template library std::sort() to sort an array declared as
int v[2000];
C++ 是否提供了一些函数可以获取数组的开始和结束索引?
Does C++ provide some function that can get the begin and end index of an array?
在 C++0x/11 中,我们得到 std::begin 和 std::end 为数组重载:
In C++0x/11 we get std::begin and std::end which are overloaded for arrays:
#include <algorithm>
int main(){
int v[2000];
std::sort(std::begin(v), std::end(v));
}
如果您无法访问 C++0x,那么自己编写它们并不难:
If you don't have access to C++0x, it isn't hard to write them yourself:
// 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;
}
这篇关于如何使用 std::sort 在 C++ 中对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
断言失败(size.width>0 && size.height&gAssertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(断言失败(size.width0 amp;amp; size.height0))
在 C++ 中旋转图像而不使用 OpenCV 函数Rotate an image in C++ without using OpenCV functions(在 C++ 中旋转图像而不使用 OpenCV 函数)
OpenCV:处理每一帧OpenCV: process every frame(OpenCV:处理每一帧)
为什么我不能在 openCV 中打开 avi 视频?Why can#39;t I open avi video in openCV?(为什么我不能在 openCV 中打开 avi 视频?)
OpenCV 无法设置 SVM 参数OpenCV unable to set up SVM Parameters(OpenCV 无法设置 SVM 参数)
使用 cvtColor 转换单一颜色Convert a single color with cvtColor(使用 cvtColor 转换单一颜色)