模数运算与三元运算

bro*_*ng0 2 c c++ if-statement modulo

我需要遍历n对整数:(0,1),(1,2),(2,3)......(n-2,n-1),(n-1,0)

最好的方法是什么?

  1. 使用模运算:

    for (int i = 0; i < n; i++){
        int a = i;
        int b = (i + 1)%n
        //MaaaanyLinesOfDoSomethingWithAAndB
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用三元运算:

    for (int i = 0; i < n; i++){
        int a = i;
        int b = (i + 1 == n ? 0 : i + 1)
        //MaaaanyLinesOfDoSomethingWithAAndB
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 要么:

    for (int i = 0; i < n; i++){
        int a = i;
        int b = (i + 1 >= n ? 0 : i + 1)
        //MaaaanyLinesOfDoSomethingWithAAndB
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 另一个想法?让我们假设有一些做某事的maaaany行,如果我们做(0,1),(1,2),(2,3)......(n-2,n-1)部分它们看起来很难看(n-1,0)部分分开.

哪种手术最有效?

编辑#1 对不起,我想我没有正确地提出我的问题.我想知道哪个操作符的动作更快(例如,秒或时钟滴答).我还决定进行一些实验,然后通过clock()函数进行测量.这是我的代码:

#include <time.h>
#include <limits.h>
#include <string>
#include <iostream>
using namespace std;

typedef void (*fun) (int a);

void DoSomething(int i){
    int a = i;
}

void ModuloOperation (int n){
    for (int i = 0; i < n; i++)
        DoSomething((i + 1) % n);
}

void TernaryEqual (int n){
    for (int i = 0; i < n; i++)
        DoSomething(i + 1 == n ? 0 : i + 1);
}

void TernaryBiggerEqual (int n){
    for (int i = 0; i < n; i++)
        DoSomething(i + 1 >= n ? 0 : i + 1);
}

void SplitIntoTwoParts (int n){
    for (int i = 0; i < n - 1; i++)
        DoSomething(i + 1);
    DoSomething(n - 1);
}

int main(){

    const int n = INT_MAX;

    string testNames[] = {
        "Modulo", 
        "Trenary equal", 
        "Trenary bigger equal", 
        "Split into two parts"
    };

    fun tests[] = {
        ModuloOperation, 
        TernaryEqual, 
        TernaryBiggerEqual, 
        SplitIntoTwoParts
    };

    clock_t t;

    for (int i = 0; i < sizeof(testNames)/sizeof(testNames[0]); i++){

        t = clock();
        tests[i](n);
        t = clock() - t;

        cout<<testNames[i]<<": "<<((float)t)/CLOCKS_PER_SEC<<" seconds\n\n";
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是一个输出

模数:53.867秒

Trenary相等:36.684秒

Trenary更大相等:37.299秒

分为两部分:31.37秒

因此,似乎pswg的想法不仅是最干净的,也是最好的.

再一次,抱歉我的错误,我不是母语,我还在学习.

p.s*_*w.g 8

你提到如果你单独做'maaaany'线会显得很难看.以上所有选项都不是特别漂亮.所以也许最好将这个丑陋的逻辑封装在一个方法中,并在循环中使用一些更优雅的代码.

为了便于阅读,我可能会这样做:

for (int i = 0; i < n - 1; i++){
    DoStuff(i, i + 1);
}
DoStuff(n - 1, 0);

// elsewhere
void DoStuff(int a, int b)
{
    //MaaaanyLinesOfDoSomethingWithAAndB
}
Run Code Online (Sandbox Code Playgroud)

如果'maaaany'行需要使用多个局部变量并且你不想将它们全部传递给DoStuff方法,你可能需要考虑使用闭包,尽管它几乎不会帮助你的代码的可读性和适当的功能分解一样多.像这样的东西:

Action<int, int> doStuff = (a, b) =>
{
    //MaaaanyLinesOfDoSomethingWithAAndB
};

for (int i = 0; i < n - 1; i++){
    doStuff(i, i + 1);
}
doStuff(n - 1, 0);
Run Code Online (Sandbox Code Playgroud)

或者你可能需要将'maaaany'行重构为一个单独的工人类,但不知道更多关于这些行中的内容,很难说.