Swift:按键引用数组元素(Swift: reference to array element by key)
我编写了自己的小函数来使用键在数组中查找元素。 但我确信在Swift中有一个可以使用的实现来将它放在一行中。 任何提示?
func objectAtKey(array: [T], key: String) -> T? { for element in array { if element.name == key { return element } } return nil }
我也知道函数indexOf,但这返回一个索引,我必须用于进一步的访问。 我认为这比较慢:
let index = array.indexOf({$0.name == key})
I have written my own small function to find an element in an array using a key. But I'm sure there is a ready to use implementation in Swift to get it in one line. Any hint?
func objectAtKey(array: [T], key: String) -> T? { for element in array { if element.name == key { return element } } return nil }
I also know function indexOf, but this return an index, I have to use for further access. I think this is slower:
let index = array.indexOf({$0.name == key})
原文:https://stackoverflow.com/questions/39236301
满意答案
优雅的解决方案是提供与比较器运算符的
intersect
(例如,在C ++中 )。
不幸的是,Matlab似乎并不支持这种功能/灵活性。解决您的问题的方法是
% convert structs into matrices A = [[a(:).x];[a(:).y]]'; B = [[b(:).x];[b(:).y]]'; % intersect the equivalent representation [C, ia, ib] = intersect( A, B, 'rows' ); % map back to original structs c = a(ia);
或者,您是否考虑使用从句柄类派生的类对象替换结构? 有可能重载类的关系运算符 ,然后应该可以直接对类对象进行排序(我没有仔细研究过这个解决方案 - 这只是我头脑中的一个提议)。
The elegant solution would have been to supply
intersect
with a comparator operator (like in , e.g., C++).
Unfortunaetly, Matlab does not seem to support this kind of functionality/flexibility.A workaround for your problem would be
% convert structs into matrices A = [[a(:).x];[a(:).y]]'; B = [[b(:).x];[b(:).y]]'; % intersect the equivalent representation [C, ia, ib] = intersect( A, B, 'rows' ); % map back to original structs c = a(ia);
Alternatively, have you considered replacing your structs with class objects derived from handle class? It might be possible to overload the relational operators of the class and then it should be possible to sort the class objects directly (I haven't looked closely into this solution - it's just a proposal off the tip of my head).
相关问答
更多如何在MATLAB中初始化一个结构体数组?(How to initialize an array of structs in MATLAB?)
在Matlab中找到两个数组结构的交集(find the intersection of two array structs in Matlab)
在matlab中找到两条线的交点(Find the intersection point of two lines in matlab)
如何在Matlab中找到两个分布的交集(How to find intersection of two distribution in Matlab)
在MATLAB中找到两个结构数组的交集(Find the intersection of two arrays of structs in MATLAB)
两个数组之间的交集[重复](Intersection between two arrays [duplicate])
使用结构组合Matlab文件(Combining Matlab files using structs)
在Matlab中找到两个正态分布的交集(Find intersection of two normal distributions in Matlab)
在MATLAB中没有for循环的多个数组的交集(Intersection of multiple arrays without for loop in MATLAB)
搜索结构的matlab单元格数组中的索引(Search indices in matlab cell array of structs)
相关文章
更多The content of element type "package" must match "...
如何取得Map当中的key
Java 数组
报错说找不到abbrev这个方法,但Array有这个方法的吧?
别拿Hadoop map key当id使
jqgrid 中组合框的值和key
自定义实现Hadoop Key-Value
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!