Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无环循环_C++ - Fatal编程技术网

C++ 无环循环

C++ 无环循环,c++,C++,我们被限制在程序中不使用循环作为编程挑战 限制: 不能使用while、for、goto和递归 这些限制相当令人畏惧。我真的想不出任何合适的解决办法。 所以我选择了这个,它是通过修改返回地址来实现的 这会更好吗 #包括 #包括 #包括 #包括 将{}无效 void magic__;(){} 无效循环{uuu} 空函数 此处为空\uu(){} 模板 结构_ { bool start=false; void*fix=nullptr; void(*body)(T&)=nullptr; 对于(无效(*正文

我们被限制在程序中不使用循环作为编程挑战

限制: 不能使用while、for、goto和递归

这些限制相当令人畏惧。我真的想不出任何合适的解决办法。 所以我选择了这个,它是通过修改返回地址来实现的

这会更好吗

#包括
#包括
#包括
#包括
将{}无效
void magic__;(){}
无效循环{uuu}
空函数
此处为空\uu(){}
模板
结构_
{
bool start=false;
void*fix=nullptr;
void(*body)(T&)=nullptr;
对于(无效(*正文)(T&)
:body(body)
{
自动为uuu做uu=uintpttr u t(为uu做u);
uint64_t magic[]={5243466812662057800,6135086863767628931ull,10416984888688609608ull,144};
mprotect((void*)(dou代表dou-dou代表dou%4096),4096,7);
std::memcpy((void*)(dou代表+135),magic,25);
}
静态无效do_(T&ctx)
{
void**p=(void**)((char*)和p+16);
如果(!ctx.started)
{
如果(!ctx)返回;
ctx.started=true;
ctx.fix=*p;
*p=(void*)do_代表;
}
主体(ctx);
ctx.next();
如果(ctx)
{
_uu;(;
魔术;
循环;
函数;
在这里;
}
其他的
{
*p=ctx.fix;
}
}
};
结构For0ToN:用于_
{
福顿(整数N,无效(*f)(福顿&)
:用于(f)
,N(N)
{
为本文件做准备;
}
运算符bool(){return istd::cout问题不清楚,但循环的一个备选方案是
std::transform()

您可以使用
longjmp
。以下是一个示例:

#包括
#包括
jmp_buf跳转缓冲区;
[[noreturn]]无效a(整数计数)
{

std::cout模板元编程是避免在代码中编写显式循环的常用方法。这项工作由编译器完成。检查阶乘和冒泡排序实现的示例,而不编写显式循环。您也可以检查堆栈溢出。

如果我将递归隐藏到函数对象中并创建有限循环,是否算数国家机器

struct state
{
    size_t current_column;
    size_t current_row;
    size_t max_column;
    size_t max_row;
};

typedef function<void(state&)> action_t;

struct do_item
{
    do_item(ostream& ss, action_t* move_next)
        : ss_(ss), move_next_(move_next) {}

    void operator()(state& s)
    {
        if (s.current_row == s.max_row)
        {
            ss_ << "done";
            return;
        }
        if (0 == s.current_column)
        {
            ss_ << s.current_row << ':';
        }
        if (s.max_column == s.current_column)
        {
            ss_ << '\n';
            s.current_column = 0;
            ++s.current_row;
            s.max_column = s.current_row;
        }
        else
        {
            ss_ << ' ' << s.current_column << '.';
            ++s.current_column;
        }
        (*move_next_)(s);
    }

    ostream& ss_;
    action_t* move_next_;
};

static string no_loops_challenge(size_t n)
{
    stringstream ss;
    state s = {0, 0, 0, n};
    action_t move_next;
    do_item action(ss, &move_next);
    move_next = action;
    action(s);
    return ss.str();
}
struct状态
{
当前列的大小;
当前行的大小;
最大列的大小;
最大行的大小;
};
类型定义功能动作;
结构do_项
{
待办事项(待办事项、行动、下一步行动)
:ss(ss),move_next(move_next){
void运算符()(状态(&s)
{
如果(s.current\u row==s.max\u row)
{

下面是一个使用range-v3的解决方案,它应该满足所有约束条件:

namespace rv = ranges::views;
    
ranges::for_each(rv::iota(0, 10), [](int i) {
    std::cout << i << ": ";
    ranges::copy(rv::iota(0, i), 
        ranges::ostream_iterator<int>(std::cout, ". "));
    std::cout << "\n";
});
namespace rv=ranges::views;
范围::每个(rv::物联网(0,10),[](int i){

std::你能提到这些限制,而不是你的实际任务是什么吗?你想解决什么问题?阅读
setjmp/longjmp
。然后立即忘记你读过的所有内容。谁设置了这个限制?上下文是什么?可能有一个“预期的”如果这是针对某个特定的类或其他对象,那么该方法是如何实现的。编译时循环使用模板会受到递归的限制吗?看,这是古人训练中的一种智慧,你会发现循环一直在你体内吗?附言。我用
longjmp
编写了一个程序,时间是25-30分钟几年前。再也不会了!我认为这是比使用
累计
为每个人
和其他方法更好的选择。@PrinzRainerBuyo据我所知(但我绝对不确定)一次只能对堆栈进行一次调用。我刚开始再次阅读有关它的内容-我突然想起了令人头痛的事情…@KhalBuyo--
longjmp
调用
setjmp
时会执行任何需要的操作来还原上下文。细节取决于编译器。@PeteBecker True,这意味着应该还原堆栈-因此,不存在仅仅因为跳远一百万次而导致堆栈溢出的风险。从我现在阅读的内容和我所做的小测试来看,这似乎是正确的。仍然是递归
namespace rv = ranges::views;
    
ranges::for_each(rv::iota(0, 10), [](int i) {
    std::cout << i << ": ";
    ranges::copy(rv::iota(0, i), 
        ranges::ostream_iterator<int>(std::cout, ". "));
    std::cout << "\n";
});