【问题标题】:What is an Alef iteration operator?什么是 Alef 迭代算子?
【发布时间】:2016-08-09 08:51:33
【问题描述】:

Alef中,迭代运算符::是什么,它有什么作用?

【问题讨论】:

    标签: loops iterator operators iteration


    【解决方案1】:

    表达式中的迭代运算符::(称为迭代表达式)允许包含语句根据其两个操作数多次执行,这两个操作数为执行提供了整数边界。它的作用就好像围绕语句构建了一个循环。例如,以下两个函数具有相同的语义:

    void foo1() { print(1::5); }
    void foo2() { print(1); print(2); print(3); print(4); }
    

    :: 的操作数在循环之前被计算,就像

    void foo3() {
        int counter = 1;
        int end = 5;
        for (; counter < end; ++counter)
            print(counter);
    }
    

    注意,也可以在每次循环迭代时将计数器值存储到某个变量中,例如,以下两个函数在语义上是等价的:

    void bar1() {
        int i;
        printTwoNumbers(i = 1::5, i);
    }
    void bar2() {
        printTwoNumbers(1, 1);
        printTwoNumbers(2, 2);
        printTwoNumbers(3, 3);
        printTwoNumbers(4, 4);
    }
    

    这可以用于巧妙的技巧,例如

    int i;
    pInt[i = 0::100] = i;
    

    pInt[0]pInt[1]pInt[2]、...、pInt[99] 处的整数初始化为012、...、99

    请参阅Alef Language Reference Manual (PDF) 以供参考。该手册还提供了两个使用迭代运算符复制字符串的简洁示例(即strcpy)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-29
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      相关资源
      最近更新 更多