#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 *からconst char *に渡す方法
タグをスパムしないでください。 CはC++はCではないではありません!それから[ask]を読んでください。あなたが求めているのは間違いです。 – Olaf