C++ 具有类似返回(C+;+;)的顺序if语句的格式

C++ 具有类似返回(C+;+;)的顺序if语句的格式,c++,dictionary,if-statement,C++,Dictionary,If Statement,在我的程序中,我使用mapmap来存储成对的类,这些类存储关于学生及其成绩的数据。重载运算符> P>我考虑如果…返回。。。else可能是混乱的编码,因为else是完全多余的 通过应用该指导原则,并使用一些明智的变量用法,您可以整理代码,使其更具可读性,例如: bool operator<(const data_t &d) { // Check means first. auto myMean = it->second.get_mean(); auto


在我的程序中,我使用map
map
来存储成对的类,这些类存储关于学生及其成绩的数据。重载<代码>运算符> P>我考虑<代码>如果…返回。。。else可能是混乱的编码,因为else是完全多余的

通过应用该指导原则,并使用一些明智的变量用法,您可以整理代码,使其更具可读性,例如:

bool operator<(const data_t &d) {
    // Check means first.

    auto myMean = it->second.get_mean();
    auto otherMean = d.it->second.get_mean();
    if(myMean < otherMean) return true;
    if(myMean > otherMean) return false;

    // Means are equal, medians comes into play.

    auto myMedian = it->second.get_median();
    auto otherMedian = d.it->second.get_median();
    if (myMedian < otherMedian) return true;
    if (myMedian > otherMedian) return false;

    // Means and medians are equal, use the names.

    return (it->first < d.it->first);
}
bool操作符second.get_-mean();
auto otherMean=d.it->second.get_mean();
如果(myMeanotherMean)返回false;
//手段平等,中位数起作用。
auto myMedian=it->second.get_median();
auto otherMedian=d.it->second.get_median();
如果(myMedianotherMedian)返回false;
//平均值和中间值相等,请使用名称。
返回(it->firstfirst);
}

<代码> > P> >我考虑<代码>如果…返回。。。else
可能是混乱的编码,因为else是完全多余的

通过应用该指导原则,并使用一些明智的变量用法,您可以整理代码,使其更具可读性,例如:

bool operator<(const data_t &d) {
    // Check means first.

    auto myMean = it->second.get_mean();
    auto otherMean = d.it->second.get_mean();
    if(myMean < otherMean) return true;
    if(myMean > otherMean) return false;

    // Means are equal, medians comes into play.

    auto myMedian = it->second.get_median();
    auto otherMedian = d.it->second.get_median();
    if (myMedian < otherMedian) return true;
    if (myMedian > otherMedian) return false;

    // Means and medians are equal, use the names.

    return (it->first < d.it->first);
}
bool操作符second.get_-mean();
auto otherMean=d.it->second.get_mean();
如果(myMeanotherMean)返回false;
//手段平等,中位数起作用。
auto myMedian=it->second.get_median();
auto otherMedian=d.it->second.get_median();
如果(myMedianotherMedian)返回false;
//平均值和中间值相等,请使用名称。
返回(it->firstfirst);
}

通常,如果你有一个
if
-
或者如果你返回bools的链,你应该把它全部折叠成
return(cond)&&&(cond2)&&&&……
或者类似的东西。关于:
return std::tie(it->second.get_mean(),it->second.get_median(),it->first)second.get_mean()),d.it->second.get_median(),d.it->first)?通常如果你有一个
if
-
或者如果你返回bools的链,你应该把它全部折叠成
return(cond)&&&(cond2)&&&……
或者类似的东西怎么样:
return std::tie(it->second.get\u mean(),it->second.get\u median(),it->first)second.get\u mean()),d.it->second.get_median(),d.it->first)