#include<iostream> using namespace std; template <class TYPE> TYPE max_of_array(TYPE *A,int len){ TYPE x=0; for(int i=0;i<len;i++){ if(A[i]>x) x=A[i]; } return x; } char* max_of_array(char* S[],int len){ } int compare_string(char* s,char* t){ for(int i=0;i<strlen(s);i++){ if(s[i]>t[i]){return 1;break; } else if(s[i]<t[i]){return -1;break; } } if(strlen(s)>strlen(t))return 1; else if(strlen(s)<strlen(t))return -1; else return 0; } int int_A[]={4,5,6,8,3,0,1,9,7,2}; double double_A[]={3.422,3.5353,3.5333,3.5666,3.23555,3.35343,3.535,3.3434,3.43,3.4}; char C[]={abc}; char D[]={abcd}; int main(){ int ix=max_of_array(int_A,10); double dx=max_of_array(double_A,10); cout<<compare(C,D)<<endl; cout<<ix<<endl; cout<<dx<<endl; }如何完善
时间: 2024-01-26 21:02:48 浏览: 24
为了完善 `max_of_array` 函数的重载版本,需要考虑如何找到字符指针数组中字典序最大的字符串。可以参考以下实现:
```cpp
char* max_of_array(char* S[], int len){
char* maxStr = S[0]; // 初始值为数组第一个元素
for(int i = 1; i < len; i++){
if(compare_string(S[i], maxStr) > 0){ // 如果当前字符串字典序大于 maxStr
maxStr = S[i]; // 更新 maxStr
}
}
return maxStr;
}
```
该函数先将 `maxStr` 初始化为数组的第一个元素,然后遍历整个数组,如果当前字符串的字典序大于 `maxStr`,则更新 `maxStr` 的值。最终返回 `maxStr` 即可。
另外,程序中使用了 `compare` 函数,但实际上是 `compare_string` 函数,需要将其改正。修改后的程序如下:
```cpp
#include<iostream>
#include<cstring>
using namespace std;
template <class TYPE>
TYPE max_of_array(TYPE *A, int len){
TYPE x = 0;
for(int i = 0; i < len; i++){
if(A[i] > x) x = A[i];
}
return x;
}
char* max_of_array(char* S[], int len){
char* maxStr = S[0];
for(int i = 1; i < len; i++){
if(compare_string(S[i], maxStr) > 0){
maxStr = S[i];
}
}
return maxStr;
}
int compare_string(char* s, char* t){
for(int i = 0; i < strlen(s); i++){
if(s[i] > t[i]){
return 1;
break;
}
else if(s[i] < t[i]){
return -1;
break;
}
}
if(strlen(s) > strlen(t)) return 1;
else if(strlen(s) < strlen(t)) return -1;
else return 0;
}
int main(){
int int_A[] = {4, 5, 6, 8, 3, 0, 1, 9, 7, 2};
double double_A[] = {3.422, 3.5353, 3.5333, 3.5666, 3.23555, 3.35343, 3.535, 3.3434, 3.43, 3.4};
char* C[] = {"abc", "def", "ghi", "jkl"};
char* D[] = {"abcd", "efgh", "ijkl", "mnop"};
int ix = max_of_array(int_A, 10);
double dx = max_of_array(double_A, 10);
char* cs = max_of_array(C, 4);
char* ds = max_of_array(D, 4);
cout << ix << endl;
cout << dx << endl;
cout << cs << endl;
cout << ds << endl;
return 0;
}
```
在 `main` 函数中,我们添加了两个字符串数组 `C` 和 `D`,并分别调用了 `max_of_array` 函数的两个版本,输出了它们的返回值。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)