2017-04-21 66 views
-2
#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 *

+1

不要发送垃圾邮件标签。 C不是C++不是C!然后阅读[问]。你要求的是明确错误的。 – Olaf

回答

0

这解释了改进:

如果初始化:char **numStr = new char*[10];
实际上,你已经有10个阵列类型char *相关。

你可以这样想想:
numStr = [addressOfChar-0,addressOfChar-1,...,addressOfChar-9];
那么对于排序功能,可以直接填写Y的开始索引和结束索引参数,如下:
sort(numStr,numStr + length,cmp);

有关的strcpy和strcat的的功能CMP的参数(int cmp(const char *str1,const char *str2)),你做不需要投到(const char **)。因为str1和str2是numStr或addressOfChar-N的元素。

对于STRCMP你有比较小于0,因为如果返回-1,结果是真实的,你可以试试下面的代码:
bool check = (-1? True: false);

你不要忘记delete所有使用内存变量堆(new),因为它会导致泄漏内存。

#include <vector> 
#include <string> 
#include <cstring> 
#include <algorithm> 
#include <iostream> 
#include <cmath> 
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,str1); 
    strcat(strNumber1,str2); 

    strcpy(strNumber2,str2); 
    strcat(strNumber2,str1); 
    return strcmp(strNumber1, strNumber2) < 0; 
} 
string PrintMinNumber(vector<int> numbers) { 
    int length = numbers.size(); 
    char **numStr = new char*[10]; 
    for(int i = 0; i < length; i++){ 
     if (numbers[i] == 0) 
     { 
      numStr[i] = new char[2]; 
     } 
     else 
     { 
      numStr[i] = new char[log(numbers[i]) + 2]; 
     } 

     sprintf(numStr[i],"%d",numbers[i]); 
    } 

    sort(numStr,numStr + length,cmp); 
    string ans = ""; 
    for(int i = 0; i < length; i++){ 
     ans += numStr[i]; 
     delete[] numStr[i]; 
    } 
    delete[] numStr; 
    return ans; 
} 
int main() 
{ 
    int a[] = { 321,3,32 }; 
    vector<int> numbers(a,end(a)); 
    cout<<PrintMinNumber(numbers); 
    delete[] strNumber1; 
    delete[] strNumber2; 
    return 0; 
} 
+0

非常感谢,它的工作原理。我很抱歉,我不能投票。 – mHuster