std::sort() 应该管用:
CArray<int> arrayOfInts;
arrayOfInts.Add(7);
arrayOfInts.Add(114);
arrayOfInts.Add(3);
std::sort(arrayOfInts.GetData(), arrayOfInts.GetData()+arrayOfInts.GetSize());
Run Code Online (Sandbox Code Playgroud)
这使用指向数组中第一个元素的指针作为开始迭代器,并指向一个超过最后一个元素的指针作为最后一个迭代器(无论如何都不应该取消引用,所以一切都很好).如果数组包含更多有趣的数据,您还可以传入自定义谓词:
struct Foo
{
int val;
double priority;
};
bool FooPred(const Foo& first, const Foo& second)
{
if ( first.val < second.val )
return true;
if ( first.val > second.val )
return false;
return first.priority < second.priority;
}
//...
CArray<Foo> bar;
std::sort(bar.GetData(), bar.GetData()+bar.GetSize(), FooPred);
Run Code Online (Sandbox Code Playgroud)
哦 - 不要用CArray.