gpt4 book ai didi

c++ - 你如何在 C++ 中比较多个字符串

转载 作者:行者123 更新时间:2023-11-28 07:01:15 25 4
gpt4 key购买 nike

我正在处理一项硬件作业,但我卡在了这部分。这是代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>

using namespace std;


std::string stringSmallest(std::string s1, std::string s2, std::string s3, std::string s4, std::string s5 );


int main(int argc, char)
{
std::string sSmallest = stringSmallest("hi", "wassup", "hello", "good","bye");
printf("strings: %i", sSmallest);
std::cin.get();
return 0;
}

std::string stringSmallest(std::string s1, std::string s2, std::string s3, std::string s4, std::string s5 ){
std::string strSmallest = 0;
if (strSmallest.compare(s1)<0)
strSmallest = s1;

if (strSmallest.compare(s2)<0)
strSmallest = s2;

if (strSmallest.compare(s3)<0)
strSmallest = s3;

if (strSmallest.compare(s4)<0)
strSmallest = s4;

if (strSmallest.compare(s5)<0)
strSmallest = s5;

return strSmallest;
}

我想做的是用最小的字符串创建一个字符串。最小 = 最小(s1, s2, s3, s4, s5)

最佳答案

试试这个

std::string stringSmallest(std::string s1, std::string s2, std::string s3, std::string s4, std::string s5 )
{
std::string strSmallest = s1;

if (strSmallest.compare(s2)>0)
strSmallest = s2;

if (strSmallest.compare(s3)>0)
strSmallest = s3;

if (strSmallest.compare(s4)>0)
strSmallest = s4;

if (strSmallest.compare(s5)>0)
strSmallest = s5;

return strSmallest;
}

关于c++ - 你如何在 C++ 中比较多个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22434299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com