首页 \ 问答 \ LIFO究竟是什么意思?(What does LIFO really mean?)

LIFO究竟是什么意思?(What does LIFO really mean?)

如本教程中所述: http//www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/

在计算机编程中,堆栈是容纳其他变量的容器(很像数组)。 但是,虽然数组允许您按照任何顺序访问和修改元素,但堆栈更受限制。 可以在堆栈上执行的操作与上面的操作相同:

1)查看堆栈顶部的项目(通常通过名为top()的函数完成)2)从堆栈中取出顶部项目(通过名为pop()的函数完成)3)在项目顶部放置一个新项目堆栈(通过名为push()的函数完成)

但是如果我在C ++中定义了两个变量,我就不必按照相同的定义顺序使用它们:

例:

int main() {
 int a;
 int b;

 b = 5;
 a = 6;
}

这段代码有问题吗? 我可以按照我喜欢的任何顺序使用它们! 我不必先使用a,然后使用b

我误会了什么吗? 它是什么?


As mentioned in this tutorial: http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/

In computer programming, a stack is a container that holds other variables (much like an array). However, whereas an array lets you access and modify elements in any order you wish, a stack is more limited. The operations that can be performed on a stack are identical to the ones above:

1) Look at the top item on the stack (usually done via a function called top()) 2) Take the top item off of the stack (done via a function called pop()) 3) Put a new item on top of the stack (done via a function called push())

But if I defined two variables in C++ I do not have to use them in the same order of definition:

Example:

int main() {
 int a;
 int b;

 b = 5;
 a = 6;
}

Is there a problem on this code? I can use them in any order I like!! I do not have to use the a first then the b.

Am I misunderstanding something? What is it?


原文:https://stackoverflow.com/questions/8425718
更新时间:2024-03-26 18:03

最满意答案

您的代码存在各种问题,但它不会引发错误,因为映射插件使用不当。

范围问题

首先, fullAddressAddress实例的属性,因此您应该在address.加上它address. 其次, with binding告诉Knockout寻找不存在的editingPerson.savePerson 。 因此,您必须将绑定更改为根范围,如下所示: click: $root.savePerson

<!-- current --> inputAddress: <input data-bind="value: fullAddress">
<!-- correct --> inputAddress: <input data-bind="value: address.fullAddress">

<!--current --> <button data-bind="click:savePerson" type="button">Save</button>
<!--correct --> <button data-bind="click:$root.savePerson" type="button">Save</button>

还建议使用对象作为构造函数参数,以便更容易与mapping plugin结合使用,并且您是否希望省略一个属性。

映射插件

映射插件文档明确指出:

对象的所有属性都将转换为可观察对象。

这意味着您不能包含计算的 observable并期望它们正常工作。 实际上,文档中有一部分关于使用计算的observable来扩充JS对象。 我可能错了,但是从我的测试和文档来看,映射的create函数似乎不能用于嵌套对象。 在此文档之后,您不需要显式创建所有可观察属性,因为对ko.mapping.fromJS的单个调用可以实例化它们。 你的新Person构造函数看起来像这样:

function Person(options){
    // because these arrays contain nested objects with computed observables,
    // they should have a special create function
    var self = this, mapping = {
        'amounts': { create: function(options) { return new Amount(options.data); }},
        'address': { create: function(options) { return new Address(options.data); }}
    };  
    // instantiates all properties, eg. surname, name, id
    ko.mapping.fromJS(options, mapping, this);
    self.fullName   = ko.computed(function() {
        return self.name()+" - "+self.surname();
    });  
}

另一个次要的'挑选':你只能在命名对象属性上使用映射插件的create函数,所以在你原来的小提琴中,插件永远不会找到persons数组,因为它是数据根。

查看完整解决方案的小提琴


There are various problems with your code, but it won't throw an error because the mapping plugin is used improperly.

The problem of scope

First, fullAddress is a property of Address instances, so you should prefix it with address. Second, the with binding tells Knockout to look for editingPerson.savePerson which doesn't exist. So you have to change the binding to the root scope, like so: click: $root.savePerson.

<!-- current --> inputAddress: <input data-bind="value: fullAddress">
<!-- correct --> inputAddress: <input data-bind="value: address.fullAddress">

<!--current --> <button data-bind="click:savePerson" type="button">Save</button>
<!--correct --> <button data-bind="click:$root.savePerson" type="button">Save</button>

It is also advised to use objects as constructor parameter, allowing for easier use in combination with the mapping plugin and should you wish to leave out one property.

The mapping plugin

The mapping plugin documentation clearly states that:

All properties of an object are converted into an observable.

This means that you cannot include computed observables and expect them to just work. Indeed, the documentation has a part about augmenting JS objects with computed observables here. I might be wrong, but from my tests and the docs, it doesn't seem so that the create functions for mapping can be used for nested objects. Following this documentation, you don't need to create all observable properties explicitly, as a single call to ko.mapping.fromJS can instantiate them. Your new Person constructor would look like this:

function Person(options){
    // because these arrays contain nested objects with computed observables,
    // they should have a special create function
    var self = this, mapping = {
        'amounts': { create: function(options) { return new Amount(options.data); }},
        'address': { create: function(options) { return new Address(options.data); }}
    };  
    // instantiates all properties, eg. surname, name, id
    ko.mapping.fromJS(options, mapping, this);
    self.fullName   = ko.computed(function() {
        return self.name()+" - "+self.surname();
    });  
}

Another minor 'nit-pick': you can only use the mapping plugin's create functions on named object properties, so in your original fiddle, the plugin will never find the persons array because it is the data root.

Check out this fiddle for the complete solution.

相关问答

更多
  • 更新: 这是我的自定义绑定的最终版本。 这现在可以自动工作,不会双重绑定,并且像'html'绑定一样工作,但更具动态性。 if (!ko.bindingHandlers['dynhtml']) { ko.bindingHandlers['dynhtml'] = { 'init': function() { return { 'controlsDescendantBindings': true }; }, 'update': fu ...
  • 我检查了小提琴,你链接的映射插件是错误的。 github中的那个不应该是一个cdn。 删除您添加的映射插件链接并将其更改为此链接 。 我还用正确的url分叉小提琴,并添加了你删除的行。 var mappedData = ko.mapping.fromJS(data); var array = mappedData(); 编辑: 要修复排序,您需要更改排序函数以调用可观察值而不是可观察函数。 喜欢来自: 来自stringSort函数的摘录: var countryA = a[column.property] ...
  • 您的代码存在各种问题,但它不会引发错误,因为映射插件使用不当。 范围问题 首先, fullAddress是Address实例的属性,因此您应该在address.加上它address. 其次, with binding告诉Knockout寻找不存在的editingPerson.savePerson 。 因此,您必须将绑定更改为根范围,如下所示: click: $root.savePerson 。 inputAddress:
    您可以使用样式绑定来更改宽度: http://jsfiddle.net/gurkavcu/GHgX7/ You can use style binding to change width : http://jsfiddle.net/gu ...
  • 我删除了一些拼写错误,一切看起来都不错,但你的模型没有意义。 function ViewModel() { var self = this; self.years = ko.observableArray(); self.statements = ko.observableArray(); this.years([ {year0: 2015, year2:16, year3: 'ABC'}, {year0: 2014, year2:15, y ...
  • 问题在于你的options绑定会尝试将它绑定的对象赋值给指定的值observable 。 例如,如果选择“A Ref Type”,则options绑定将推送json对象 { "Id":2, "Name":"A Ref Type" } 进入你的Task.ReferenceTypeId observable,然后将其序列化回你的服务器。 在这种情况下,您需要添加一个optionsValue配置选项来告诉绑定仅用于保存该id。