简体   繁体   English

按索引范围从向量中选择元素

[英]Selecting elements from vector by index range

Despite some googling I can't find anything about how to do this.尽管进行了一些谷歌搜索,但我找不到有关如何执行此操作的任何信息。

If I want to take some elements of an array I can do如果我想获取数组的一些元素,我可以做

var partOfArray = myArray[firstIndex..lastIndex];
var anotherpartOfArray = myArray[firstIndex..^1];

Can I do the same for something of type Vector?我可以对 Vector 类型的东西做同样的事情吗? Something like就像是

// Vector<double> myVector; 
var partOfVector = myVector(firstIndex..lastIndex);

Currently my only workaround is to convert the vector back to an array, index it, then convert it back to a vector.目前我唯一的解决方法是将向量转换回数组,索引它,然后将其转换回向量。 There must be an easier way to do this.必须有一种更简单的方法来做到这一点。

This does not seem to be available due to the "Countable" requirement for using implicit range indexing :由于使用隐式范围索引的“可计数”要求,这似乎不可用:

  1. The type is Countable.类型是可数的。
  2. The type has an accessible instance indexer which takes a single int as the argument.该类型有一个可访问的实例索引器,它采用单个 int 作为参数。
  3. The type does not have an accessible instance indexer which takes an Index as the first parameter.该类型没有可访问的实例索引器,它将索引作为第一个参数。 The Index must be the only parameter or the remaining parameters must be optional. Index 必须是唯一的参数,或者其余参数必须是可选的。

"Countable" in this context means having a Count or Length instance property, Vector<T> only have a static Count property.在此上下文中,“可Count ”意味着具有CountLength实例属性, Vector<T>仅具有静态 Count 属性。

Note that Vector<T> is intended for high performance SIMD optimizations, ie it is not equivalent to a c++ vector, and this unfortunately often means convenience will be sacrificed for performance.请注意, Vector<T>旨在用于高性能 SIMD 优化,即它等同于 c++ 向量,不幸的是,这通常意味着为了性能而牺牲便利性。 If you are converting the vector back to an array and back again will probably significantly reduce the benefit of vectorization.如果您将向量转换回数组并再次返回,则可能会显着降低向量化的好处。 It is difficult to provide better suggestions without knowing the specific application.在不了解具体应用的情况下,很难提供更好的建议。

I would also suggest reading about SIMD intrinstics .我还建议阅读SIMD 内在函数 My takeaway from that article is that the gains from Vector<T> is rather modest compared to that of intrinstics, even if the later ends up being platform specific.我从那篇文章中得出的结论是,与内部函数相比, Vector<T>的收益相当有限,即使后者最终是特定于平台的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM