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.

相关问答

更多

KnockoutJS:使用'html'绑定,新元素不具有约束力(KnockoutJS: Using 'html' binding, new elements not binding)

更新: 这是我的自定义绑定的最终版本。 这现在可以自动工作,不会双重绑定,并且像'html'绑定一样工作,但更具动态性。 if (!ko.bindingHandlers['dynhtml']) { ko.bindingHandlers['dynhtml'] = { 'init': function() { return { 'controlsDescendantBindings': true }; }, 'update...

如何使用knockoutjs映射插件对数据绑定到表?(How to sort data bind to table using knockoutjs mapping plugin?)

我检查了小提琴,你链接的映射插件是错误的。 github中的那个不应该是一个cdn。 删除您添加的映射插件链接并将其更改为此链接 。 我还用正确的url分叉小提琴,并添加了你删除的行。 var mappedData = ko.mapping.fromJS(data); var array = mappedData(); 编辑: 要修复排序,您需要更改排序函数以调用可观察值而不是可观察函数。 喜欢来自: 来自stringSort函数的摘录: var countryA = a[column.prop...

knockoutjs映射嵌套元素不绑定(knockoutjs mapping nested elements is not binding)

您的代码存在各种问题,但它不会引发错误,因为映射插件使用不当。 范围问题 首先, fullAddress是Address实例的属性,因此您应该在address.加上它address. 其次, with binding告诉Knockout寻找不存在的editingPerson.savePerson 。 因此,您必须将绑定更改为根范围,如下所示: click: $root.savePerson 。 <!-- current --> inputAddress: <input data-bind="val...

knockoutjs中的双向绑定(Two way binding in knockoutjs)

您可以使用样式绑定来更改宽度: <input data-bind="value:width ,style : { width : width()+'px' }" /> http://jsfiddle.net/gurkavcu/GHgX7/ You can use style binding to change width : <input data-bind="value:width ,style : { width : width()+'px' }" /> http://jsfiddle.n...

在knockoutJS中嵌套的foreach绑定(Nested foreach binding in knockoutJS)

我删除了一些拼写错误,一切看起来都不错,但你的模型没有意义。 function ViewModel() { var self = this; self.years = ko.observableArray(); self.statements = ko.observableArray(); this.years([ {year0: 2015, year2:16, year3: 'ABC'}, {year0: 2014, year2:...

knockoutjs映射选择数据绑定选项(knockoutjs mapping select data-bind options)

问题在于你的options绑定会尝试将它绑定的对象赋值给指定的值observable 。 例如,如果选择“A Ref Type”,则options绑定将推送json对象 { "Id":2, "Name":"A Ref Type" } 进入你的Task.ReferenceTypeId observable,然后将其序列化回你的服务器。 在这种情况下,您需要添加一个optionsValue配置选项来告诉绑定仅用于保存该id。 <select data-bind="options: Reference...

Knockoutjs映射 - 没有初始数据(Knockoutjs mapping - without initial data)

我通过将一个空数据模型发送到视图(这是.NET MVC)作为ViewBag属性的一部分,并使用名为Ngon的库(https://github.com/brooklynDev/NGon)解决了这个问题。 public ViewREsult Index() { var massiveDataModel = new MassiveDataModel(); ViewBag.NGon.Stuff = massiveDataModel; return View(); } 并在脚本...

KnockoutJS:为虚拟元素使用嵌套的条件if语句(KnockoutJS: Using nested conditional if statements for virtual elements)

问题在于...... <!-- ko if: noitems --> <!-- ko foreach: noitems --> <span data-bind="text: $data"></span> <!-- /ko --> <!-- /ko --> 您需要在视图模型中定义noitems成员。 编辑 你可能会做这样的事情来解决它 - 我没有尝试过。 <!-- ko if: noitems --> <!-- ko foreach: noitems && ...

knockoutjs在html上使用逻辑或启用绑定(knockoutjs use logical or with enable binding on html)

每当有多个条件时,使用括号(例如box1()|| box2())。 像这样绑定: - <select id="main-select-1" data-bind="options: workouts, optionsText: 'WorkoutName', value: selectedWorkout, enable: box1() || box2()"></select> 小提琴演示 Whenever there are more then one condition, use paren...

KnockoutJS Mapping toJS忽略嵌套键(KnockoutJS Mapping toJS ignore nested keys)

如果您删除父项(如果您知道您的密钥在obj中是唯一的,或者如果您想要删除所有出现的),它会起作用: var obj = { "akey": { "anestedkey": "ignore", "anotherkey": "value" } }; console.log(ko.mapping.toJS(obj, { "ignore": ["anestedkey"], //here })); http://jsfiddle.net/Gabri...

相关文章

更多

Hadoop究竟是什么?

  文章的标题看上去很幼齿很科普,但实际上那些天天观赏大数据演讲的管理层技术小白来说,他们永远不会公开 ...

Collections.unmodifiableMap 这个是什么意思

这个是什么意思 不可改变????

ext 的store中 baseParams.conditions 是什么意思啊?

小弟最近做个练习 在表格分页的时候 有个很帅的分页工具栏 最右边是一个刷新的按钮 调了下 发现他是 ...

这些是什么

用ROR做了一个 图书管理系统 在公司内网机 执行程序就什么都没有 回家在自己的笔记本上 执行程序 ...

弱弱的问一下 java 中的 <= 表示什么意思?

下面代码是什么意思? public boolean test(double x){ retu ...

struts2.xml中crud!input什么意思

在看struts2自带的crud的例子中,struts.xml中的crud!input、crud!de ...

java是什么

java大学网是以java开发为主线,与java相关编程、视频、图书为辅的一个专题化的教程多站。所以, ...

Hive是什么?

hive是什么? hive是基于Hadoop构建的数据仓库基础架构,通过提供一系列的工具,使得用户能够 ...

shell是什么

shell简单理解就是类似于一个客户端窗口,用户输入指令,它解析好(操作系统能读得懂),然后传给操作系 ...

JSON是什么?

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅 ...

最新问答

更多

如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)

请尝试以下方法: $messages = $user->contact_messages()->paginate(10); Try the following: $messages = $user->contact_messages()->paginate(10);

linux的常用命令干什么用的

linux和win7不一样,win7都图形界面,都是用鼠标来操作打开,解压,或者关闭。而linux是没有图形界面的,就和命令提示符一样的一个文本框,操作linux里的文件可没有鼠标给你用,打开文件夹或者解压文件之类的操作都要通过linux常用命令。

由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)

它创建了这些视图: 'auth/login.blade.php', 'auth/register.blade.php', 'auth/passwords/email.blade.php', 'auth/passwords/reset.blade.php', 'layouts/app.blade.php', 'home.blade.php' 并修改这些文件: 'Http/Controllers/HomeController.php', 'routes/web.php' 如果使用--views

如何交换返回集中的行?(How to swap rows in a return set?)

您可以使用特殊的CASE表达式进行ORDER BY如下所示: SELECT * FROM table ORDER BY CASE id WHEN 3 THEN 8 WHEN 8 THEN 3 ELSE id END You can ORDER BY using a special CASE expression like this: SELECT * FROM table ORDER BY CASE id WHEN 3 THEN 8 WHEN 8 THEN 3 ELSE id END

在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)

我建议你制作一个新的TableView Cell,在这个newTableViewCell中,加载第2节中的所有单元格,然后为newTableViewCell提供边框。 但如果您不想这样做,那么您可以使用以下代码: @property(strong,nonatomic) CAShapeLayer* borderPath; -(void)viewDidLayoutSubviews{ [_borderPath removeFromSuperlayer]; UIView *viewToGiveBord

使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)

出于一些奇怪的原因,我现在发现了一个不同的问题, Boost.Spirit SQL语法/词法分析失败 ,其中提供了一些其他解决方案来进行空格跳过。 一个更好的! 以下是根据建议重新编写的示例代码: #include #include #include #include #include #incl

Java中的不可变类(Immutable class in Java)

1.如果我只有final变量的课程? 这会让你远离但不是全部。 这些变量的类型也需要是不可变的。 考虑一下 class MyImmutableClass { // final variable, referring to a mutable type final String[] arr = { "hello" }; // ... } 这允许有人做 myImmutableObject.arr[0] = "world"; 并有效地改变你的不可变类的对象。 此外,建议禁

WordPress发布查询(WordPress post query)

如果你想要在div中包含所有帖子: post_parent) { $myposts = get_posts('numberposts=10&tag=medical&order=DESC'); echo ' '; foreach ($myposts as $post): ?>

如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)

我不确定哪个是MySQL的正确答案,因为它本身还不支持IPv6地址格式(尽管“ WL#798:MySQL IPv6支持 ”表明它将在MySQL v6.0中,当前文档不支持)。 不过,你建议的人建议去2 * BIGINT,但要确保他们是UNSIGNED。 在IPv6的/ 64地址边界处有一种自然分割(因为/ 64是最小的网格块大小),这将与其很好地对齐。 I'm not sure which is the right answer for MySQL given that it doesn't y

是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)

您可以使用Object.keys和Array#find来获取匹配value的key 。 const letters = {a:'26',b:'25',c:'24',d:'23',e:'22',f:'21',g:'20',h:'19',i:'18',j:'17',k:'16',l:'15',m:'14',n:'13',o:'12',p:'11',q:'10',r:'9',s:'8',t:'7',u:'6',v:'5',w:'4',x:'3',y:'2',z:'1'}; function sw