C++ 一起初始化引用和分配变量

Dr *_*hil 0 c++

我的代码中有这一部分,我在同一布尔条件下使用 if else 和三元运算符。有没有更优雅的方法来做到这一点?

bool UseGroups //input parameter to a function.

    std::vector<std::vector<int>>& relevantGamesGroup = (useGroups) ? (objFlight.gamesGroup[dayIndex]) : (objFlight.gamesSubGroups[dayIndex]);

    if (useGroups) {
        numberOfGroups = objFlight.numberOfGroups[dayIndex];
    }
    else {
        numberOfGroups = 2 * (objFlight.numberOfGroups[dayIndex]);
    }
Run Code Online (Sandbox Code Playgroud)

rtu*_*ado 5

我可能会这样写,因为我觉得读起来很清楚:

auto& relevantGamesGroup = useGroups
    ? objFlight.gamesGroup[dayIndex]
    : objFlight.gamesSubGroups[dayIndex];
auto numberOfGroups = useGroups
    ? objFlight.numberOfGroups[dayIndex]
    : objFlight.numberOfGroups[dayIndex] * 2;
Run Code Online (Sandbox Code Playgroud)