简体   繁体   English

C++ 字符 Class 运算符< function 比较

[英]C++ Char Class Operator< function compare

bool String::operator < (const String &s) const 
{
int len1 = s.getLength();
int len2 = this->getLength();
int cap;
if(len1>len2) cap = len2;
else cap = len1;
int index=0;
for (int i =0; i < cap;i++)
    if (this->buffer[i] != s[i])
        index =i;
for (int i= index; i < cap; i++)
    if (this->buffer[i] < s[i])
        return true;
for (int i= index; i < cap; i++)
    if (this->buffer[i] > s[i])
        return false;
if (*this == s)
    return false;
if (len2>len1)
    return false;
if (len2<len1)
    return true;
}

I have been working with this overloading operator< that compares two strings.我一直在使用比较两个字符串的重载运算符<。

It's working successfully.它工作成功。 I test every case that possible.我尽可能地测试每一个案例。

But when I F7 the program, it still shows up 'String::operator<': not all control paths return a value.但是当我 F7 程序时,它仍然显示'String::operator<':并非所有控制路径都返回一个值。

Please tell me what am I missing in this function?请告诉我在这个 function 中我缺少什么?

If your loops don't return , and if *this == s is false (you shouldn't be doing that comparison in this function), and if len2>len1 and len2<len1 are both false (meaning len2==len1 is true), then you are indeed missing a final return , which is what the compiler is warning you about.如果您的循环不return ,并且如果*this == s为假(您不应该在此函数中进行比较),并且如果len2>len1len2<len1都为假(意味着len2==len1是true),那么您确实缺少最终的return ,这是编译器警告您的内容。

bool String::operator < (const String &s) const 
{
    ...

    if (len2>len1)
        return false;
    if (len2<len1)
        return true;

    // IF YOU REACH HERE, THERE IS NO RETURN!
}

That being said, your code can be simplified a bit:话虽如此,您的代码可以简化一点:

bool String::operator < (const String &s) const 
{
    int len1 = this->getLength();
    int len2 = s.getLength();
    int cap = (len1 < len2) ? len1 : len2;
    for (int i = 0; i < cap; ++i) {
        if (this->buffer[i] != s[i]) {
            return (this->buffer[i] < s[i]);
        }
    }
    return (len1 < len2);
}

Alternatively, you can use std::mismatch() :或者,您可以使用std::mismatch()

#include <algorithm>

bool String::operator < (const String &s) const 
{
    int len1 = this->getLength();
    int len2 = s.getLength();
    int cap = std::min(len1, len2);
    const char* buffer_end = this->buffer + cap;
    std::pair<const char*, const char*> ret = std::mismatch(this->buffer, buffer_end, s.buffer);
    return (ret.first != buffer_end)
        ? (*ret.first < *ret.second)
        : (len1 < len2);
}

Or, in C++14 and later:或者,在 C++14 及更高版本中:

#include <algorithm>

bool String::operator < (const String &s) const 
{
    int len1 = this->getLength();
    int len2 = s.getLength();
    const char* buffer_end = this->buffer + len1;
    const char* s_end = s.buffer + len2;
    auto ret = std::mismatch(this->buffer, buffer_end, s.buffer, e_end);
    return ((ret.first != buffer_end) && (ret.second != s_end))
        ? (*ret.first < *ret.second)
        : (len1 < len2);
}

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

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