C ++指向布尔值的指针(C++ pointer to boolean value)
我在尝试实现一个包含指向布尔值的指针的类时遇到问题...
class BoolHolder { public: BoolHolder(bool* _state); bool* state; } BoolHolder::BoolHolder(bool* _state) { state = _state; } bool testbool = false; BoolHolder testbool_holder( &testbool );
如果我这样做,testbool_holder.state总是报告它是真的,无论testbool本身是真还是假
我究竟做错了什么? 我只是希望类能够保持testbool的最新值,但我不知道如何实现这一点。 谢谢
I'm having problems trying to implement a class which contains a pointer to a boolean...
class BoolHolder { public: BoolHolder(bool* _state); bool* state; } BoolHolder::BoolHolder(bool* _state) { state = _state; } bool testbool = false; BoolHolder testbool_holder( &testbool );
If I do this, testbool_holder.state always reports that it is true, no matter whether testbool itself is true or false
What am I doing wrong? I just want the class to be able to maintain an up to date value for testbool but I don't know how to effect this. Thanks
原文:https://stackoverflow.com/questions/18559572
最满意答案
在递归访问期间继续执行第一个元素,最后停止:
compare_adj([First|Rest]) :- compare_adj([First|Rest], First). compare_adj([A,B|T], F) :- writeln(compare(A,B)), compare_adj([B|T], F). compare_adj([Last], First) :- writeln(compare(Last, First)). ?- compare_adj([5, 4, 8, 9, 7, 6]). compare(5,4) compare(4,8) compare(8,9) compare(9,7) compare(7,6) compare(6,5) true ; false.
编辑
虽然效率很低,但我们可以将一些内置组合起来,以节省一些代码:
compare_adj([First|Rest]) :- append([First|Rest],[First],Temp), forall(append(_, [A,B|_], Temp), writeln(compare(A,B))).
just carry on the first element during a recursive visit, stopping at last:
compare_adj([First|Rest]) :- compare_adj([First|Rest], First). compare_adj([A,B|T], F) :- writeln(compare(A,B)), compare_adj([B|T], F). compare_adj([Last], First) :- writeln(compare(Last, First)). ?- compare_adj([5, 4, 8, 9, 7, 6]). compare(5,4) compare(4,8) compare(8,9) compare(9,7) compare(7,6) compare(6,5) true ; false.
edit
albeit fairly inefficient, we could do combining some builtins, to spare some code:
compare_adj([First|Rest]) :- append([First|Rest],[First],Temp), forall(append(_, [A,B|_], Temp), writeln(compare(A,B))).
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
循环链表没有真正的开头和结尾。 但是从你的评论中,我想你想要在循环浏览列表时检测到你开始使用的元素。 #The structure of ListNode class ListNode: def __init__(self, val): self.val = val self.next = None # Supposes you have a circular linked list and you have a reference to head. You can do as fol ...
-
一个简单的解决方案是: list(nil, []). list(cons(A,X), [A|L]):- list(X,L). 例: ?- list( cons(a, cons(b, cons(c, cons(d, nil )))), L). L = [a, b, c, d]. A simple solution would be: list(nil, []). list(cons(A,X), [A|L]):- list(X,L). Example: ?- list( cons(a, c ...
-
在prolog中过滤列表(Filter list in prolog)[2022-03-14]
对于现有的答案,我想添加一个相当一般的答案,因为您可以在多个方向使用它。 构建块: list_element_number/3 我从下面的谓词开始,定义了以下关系 : 一个列表 Ls0 元素 E 在Ls0中出现E的Ls0 这里是: list_element_number(Ls0, E, N) :- tfilter(=(E), Ls0, Ls), length(Ls, N). 该解决方案使用library(reif) tfilter/3 。 谓词包含您发布的函数count 。 ... -
如果你可以改变列表,这是一个标准的方法: (define (make-circular lst) ; helper for finding the last pair in a list (define (last-pair lst) (if (null? (cdr lst)) lst (last-pair (cdr lst)))) ; special case: if the list is empty (cond ((null? lst ...
-
在递归访问期间继续执行第一个元素,最后停止: compare_adj([First|Rest]) :- compare_adj([First|Rest], First). compare_adj([A,B|T], F) :- writeln(compare(A,B)), compare_adj([B|T], F). compare_adj([Last], First) :- writeln(compare(Last, First)). ?- compare_adj([5, 4, 8, 9, 7, 6 ...
-
prolog中的列表清单?(List of lists in prolog?)[2023-10-24]
我们分两步完成。 首先,让我们得到一个列表的最大值: maxlist([X,Y|Z], Max) :- X > Y -> maxlist([X|Z], Max) ; maxlist([Y|Z], Max). maxlist([X], X). 看起来不错: ?- maxlist([1,5,7,3,5], Max). Max = 7. 让我们使用maplist/3将它应用于二维列表。 每个最大值的最大值将是总体最大值: max2D(List, Max) :- ... -
严格地说,当你想表达关于列表中所有(或足够多)元素的某种关系时,你怎么能避免递归。 但是,您可以将递归部分委托给某个库谓词。 以maplist-family为例。 说maplist(=(_), Xs) ,它描述了元素相同的所有列表。 对你而言,不再有任何递归。 但后面有一个递归定义: maplist(_C_1, []). maplist(C_1, [E|Es]) :- call(C_1, E), maplist(C_1, Es). 还有其他方法可以实现递归谓词的快捷方式,例如do-loops和 ...
-
如何将从输入读取的字符串转换为prolog中的列表列表(How to convert a string read from input to a list of lists in prolog)[2022-08-15]
看看这个答案 。 我在那里写的代码在空格上分割Prolog字符串以生成原子列表。 通过一个小的修改,您可以更改代码以创建字符串而不是原子。 以下是上一篇文章中的相关代码,并对您的案例进行了必要的修改: data([A|As]) --> spaces(_), chars([X|Xs]), {string_to_list(A, [X|Xs])}, %% Using string_to_list/2 instead spaces(_), data(As). dat ...