C++字符串如何比较大小


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<string.h>
using namespace std;

int max(int a, int b)
{
    if (a > b)
        return a;
    else
        return b;
}

float max(float a, float b)
{
    if (a > b)
        return a;
    else
        return b;

}

char* max(char* a, char* b)
{
    char p[40];
    strcpy(p, a);
    strcpy(a, b);
    strcpy(b, p);
    int ret = strcmp(a, b);
    if (ret < 0)
    {
        return b;
    }
    else 
    {
        return a;
    }
}

int main()
{
    int a, b;
    float c, d;
    char e,  f;
    

    int max(int a, int b);
    float max(float a, float b);
    char* max(char* a, char* b);
    cout << "请输入两个数字:";
    cin >> a >> b;
    cin >> c >> d;
    cin >> e >> f;
    cout<<"两个数中的较大者为:"<<max(a, b) << endl;
    cout << "两个数中的较大者为:" << max(c, d) << endl;
    cout << "两个数中的较大者为:" << max(e, f) << endl;

    return 0;
}

返回了局部变量地址,自然出错

你好,如有帮助,请采纳一下,谢谢!
字符串这样比较

#include <iostream>
using namespace std;
int main(){
    string str1="hello";
    cout<<str1.compare("helloo")<<endl;//返回-1; 
    cout<<str1.compare("hello")<<endl;//返回0 ; 
    cout<<str1.compare("hell")<<endl;//返回1; 
}