相关疑难解决方法(0)

在C++中n = {0,1,...,n-1}

自然数n的形式定义(在集合论中)如下:

  • 0是空集
  • 1 = {0}
  • n = {0,1,...,n-1}

我认为这会使一些C++代码更简单,如果我被允许这样做:

for (int n : 10)
    cout << n << endl;
Run Code Online (Sandbox Code Playgroud)

它打印的数字从0到9.

所以我尝试执行以下操作,但不编译:

#include <iostream>
#include <boost/iterator/counting_iterator.hpp>


    boost::counting_iterator<int> begin(int t)
    {
        return boost::counting_iterator<int>(0);
    }

    boost::counting_iterator<int> end(int t)
    {
        return boost::counting_iterator<int>(t);
    }



int main() 
{
    for (int t : 10)
        std::cout << t << std::endl;

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

有关如何实现这一目标的任何建议?我用clang ++得到以下错误:

main.cpp:22:20: error: invalid range expression of type 'int'; no viable 'begin' function available
        for (int t : 10)
                ^ ~~
Run Code Online (Sandbox Code Playgroud)

但我想我应该被允许这样做!:)

编辑:我知道如果我在for循环中添加"range"(或其他一些单词)这个词,我可以"伪造"它,但我想知道是否可以不使用它.

c++ set-theory c++14

0
推荐指数
1
解决办法
1103
查看次数

标签 统计

c++ ×1

c++14 ×1

set-theory ×1