#include "stdafx.h"
#include <vector>
#include <string>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
int len = 10;
char * strNumber1 = new char[2*len+1];
char * strNumber2 = new char[2*len+1];
int cmp(const char *str1,const char *str2){
strcpy(strNumber1,*(const char**)str1);
strcat(strNumber1,*(const char**)str2);
strcpy(strNumber2,*(const char**)str2);
strcat(strNumber2,*(const char**)str1);
return strcmp(strNumber1,strNumber2);
}
string PrintMinNumber(vector<int> numbers) {
int length = numbers.size();
char **numStr = new char*[10];
for(int i = 0; i < length; i++){
sprintf(numStr[i],"%d",numbers[i]);
}
sort((char*) numStr[0],(char*)numStr[length],cmp);
// I don't know how to pass the char* from char** numStr;
string ans = "";
for(int i = 0; i < length; i++){
ans += numStr[i];
}
return ans;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[3] = {3,32,321};
vector<int> numbers(a,a+3);
cout<<PrintMinNumber(numbers);
return 0;
}
以上是我用来解决问题的代码,它是如何从3,321,32中获得最小数目的。我们可能得到的结果是321323.所以我需要对字符串进行排序,但我不知道如何将char *
从char**
传递到const char*
。你能解释我需要做什么吗?如何将char *从char **传递到const char *
不要发送垃圾邮件标签。 C不是C++不是C!然后阅读[问]。你要求的是明确错误的。 – Olaf