简体   繁体   English

C ++中的字符串比较如何工作?

[英]How does strings comparison in C++ work?

I am trying to solve this problem . 我正在努力解决这个问题

I am implementing it with strings. 我正在用字符串实现它。 Here is my code snippet 这是我的代码片段

string s,ss;
// s and ss both contains integer input.

while(s <= ss )
//while( s<=ss && s.size() <= ss.size())
{
    int i = inc, j = dec;   // inc and dec are middle values. both equal if odd else different

    while((s[j]-'0')==9 && i < len && j>=0){
        // for cases like 999
        s[i] = s[j] = '0';
        i++;
        j--;
    }
    if(j<0){
        s = "1" + s;
        int l = s[len-1] - '0';
        l++;
        //cout<<l<<"\n";
        s[len] = (l + '0');
    }
    else{
        int l = s[j] - '0';
        l++;
        s[i] = s[j] = (l+'0');
    }

    if(s <= ss)
        cout<<"out in wild "<<s<<" and "<<ss<<"\n";
}

cout<<s<<endl;

The problem that I am facing is when input is like 999 or 9999. The outer while loop keeps on looping even when the value of s increases, but if I add while( s<=ss && s.size() <= ss.size()) it works completely fine. 我面临的问题是输入类似999或9999时。即使s的值增加,外部的while循环也会继续循环,但是如果我加上while( s<=ss && s.size() <= ss.size())完全正常。 Why is while(s<=ss) is not working? 为什么while(s <= ss)不起作用? I rarely use the string class, so I don't understand it completely. 我很少使用string类,因此我不太了解它。 Why don't string s= 101 and ss=99 stop the while loop? 为什么不让string s= 101ss=99停止while循环?

Complete code link is here 完整的代码链接在这里

You are comparing strings with lexicographical order , not numbers , so "101" is less than "99" (because '1' < '9') , eg 您要比较的是按字典顺序而不是数字的字符串,因此“ 101”小于“ 99”(因为'1'<'9'),例如

int main(){
    std::string s = "99";
    std::string ss = "101";

    std::cout << std::boolalpha << (s <= ss);  
}

Outputs false . 输出false

Notes: 笔记:

  • A better design for your program would be to manipulate numbers ( int or double ...) and not strings in the first place, so this kind of expressions would naturally work as you expect. 程序的更好设计是首先处理数字( intdouble ...),而不要处理字符串,因此,这种表达式自然可以按您期望的那样工作。

    Eg "101" + "99" is "10199", not "200" ... 例如,“ 101” +“ 99”是“ 10199”,而不是“ 200” ...

  • But if you really need strings, consider this post to sort strings containing numbers. 但是,如果您确实需要字符串,请考虑这篇文章以对包含数字的字符串进行排序。

  • As pointed by @Deduplicator, a program that needlessly overuses strings is sometimes called Stringly Typed 如@Deduplicator所指出的,不必要地过度使用字符串的程序有时称为Stringly Typed

  • Also see std::lexicographical_compare 另请参见std::lexicographical_compare


Since your input explicitly only involves positive integers without leading 0 , writing a comparison function is trivial, something like : (untested) 由于您的输入明确只包含不带前导0的正整数 ,因此编写比较函数很简单,例如:(unested)

/* Returns 1 if the integer represented by s1 > the integer represented by s2
*  Returns -1 if the integer represented by s1 < the integer represented by s2
*  Return 0 is both are equals
*
*  s1 and s2 must be strings representing positive integers without trailing 0
*/
int compare(const std::string& s1, const std::string& s2)
{
  if(s1.size() > s2.size())
      return 1;
  if(s2.size() > s1.size())
      return -1;

  for(std::size_t i = 0 ; i < s1.size() ; ++i)
  {
    if(s1[i] - '0' < s2[i] - '0')
      return 1;
    if(s2[i] - '0' < s1[i] - '0')
      return -1;
  }

  return 0;
}

While s and ss are string variables, they are compared character by character. 尽管sss是字符串变量,但将它们逐个字符地进行比较。

In the case that you mentioned being: s = "101" & ss = "99" , by first hand it will check the first character in each string, and as '1' < '9' it exit up with s < ss . 如果您提到的是: s = "101"ss = "99" ,则第一手将检查每个字符串中的第一个字符,并且当'1' < '9'时以s < ss退出。 I would advise you to convert those values to integers before comparison. 我建议您在比较之前将这些值转换为整数。

由于按字母顺序将s与ss进行比较,我建议您将尾部的一个字符与头部的一个字符进行比较(一个到一个地到达中间),以解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM