把一个数组排序为最小的值

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<sstream>
using namespace std;

static bool cmp(int a, int b)//这里的 cmp函数sort中的比较函数compare要声明为静态成员函数或全局函数,不能作为普通成员函数,否则会报错。 因为:非静态成员函数是依赖于具体对象的,而std::sort这类函数是全局的,因此无法再sort中调用非静态成员函数。静态成员函数或者全局函数是不依赖于具体对象的, 可以独立访问,无须创建任何对象实例就可以访问。同时静态成员函数不可以调用类的非静态成员。

{

string A = "";
string B = "";
A += to_string(a);
A += to_string(b);
B += to_string(b);
B += to_string(a);


return A<B;
}
string PrintMinNumber(vector<int> numbers) {
string  answer = "";
sort(numbers.begin(), numbers.end(), cmp);
/*for (int i = 0; i < numbers.size(); i++)
cout << numbers[i] << endl;*/
for (int i = 0; i<numbers.size(); i++){
answer += to_string(numbers[i]);
}
return answer;
}

/*
int toint(string s)
{
int num = 0;
stringstream ss(s);

ss >> num;
return num;


}
string tostring(int a)
{
string s;
stringstream ss;
ss << a;
return ss.str();

}*/这段不用看,自己写写试试
void main()
{
string s;
int a[3] = {5,12,3};
vector<int>v(a,a+3);
for (int i = 0; i < v.size(); i++)
cout << v[i] << endl;
s = PrintMinNumber(v);
cout << s << endl;
/*string s2 = "78";
string s1 = "69";
int c1=89, c2=78;
s1 = tostring(c1);
s2 = tostring(c2);
s1 = s1 + s2;
cout << s1<< endl;*/
system("pause");




}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值