使用sort函数对数组排序
按照升序将数组的数字排序:
/********************************************************
@File sort function.
@Description sort from small to large.
@Author Eric Wang
@Date: 2020-01-08
@Association: Harbin Institute of Technology, Shenzhen.
*********************************************************/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(int& x, int& y)
{
return x < y;
}
class Solution
{
public:
int singleNumber(vector<int>& nums)
{
sort(nums.begin(), nums.end(), compare);
}
};
int main(int argc, char** argv)
{
Solution solution;
vector<int> num = {1, 2, 1, 3, 5};
solution.singleNumber(num);
for(vector<int>::iterator it = num.begin(); it != num.end(); ++it)
{
cout << *it << endl;
}
}
输出结果如下:
需要注意的,compare
函数需要定义为全局函数,或者类静态成员函数。
来源:CSDN
作者:EricWangx
链接:https://blog.csdn.net/weixin_39374743/article/details/103883922