Vector类用法 Vector Class

本文讲述关于Vector类的概念、命名空间、初始化、属性、方法、实例、常见问题等。

Description

A Vector is a special type of array. The important points about a Vector are:

  • Each item in the Vector is referenced by an integer index, starting with ‘0’.
  • It is dynamic (doesn’t have a fixed size).
  • Vectors have a ‘front’ which is position ‘0’. And a ‘back’ which is the last position in the Vector.
  • Vectors can contain normal datatypes (such as int, string, double, bool) or other Classes.
  • When a new item is inserted into a Vector, the index of all the current items after that index are increased by one.
  • When an item is erased from a Vector, the index of all the items after that index are decreased by one.
  • Vectors don’t have a history, so you can’t refer to a vector[1] meaning one bar ago. This is an important difference from arrays.

Important programming notes:

  • Whenever you add or remove an item from a Vector it changes the size of the Vector. It is therefore very important to check the size of a Vector before trying to access it.
  • Whenever you add or remove an item from a Vector it can change the indexed position of some or all the elements. Vectors are therefore not generally suitable for data that needs to be in a fixed position.
  • If you insert or remove an item, all indexes after that point is shuffled up or down. This can be very CPU intensive if you insert or remove an item at a low position in a large vector.
  • Vectors can only be accessed from the ‘front’, the ‘back’ or via an index value.

Namespace

     elsystem.collections

Initialize

using elsystem.collections;

var: Vector vect( NULL );

vect = new Vector;

Like all Classes, vect is automatically declared as IntrabarPersist.

Properties

PropertyTypeDescriptionExample
vect.count<int>Returns the number of items in the Vector. In the TS documentation the word ‘size’ is used but this is incorrect. 
vect.items<object>Can be used to set the value of an item in the Vector.vect.Items[4]=”peter”;

Methods

PropertyDescriptionReturn valueExample
vect.atReturns an item at a specific index.Normal datatype (int, bool, string etc) or object.
You should use the astype command to ensure the datatype is returned correctly.
mystring = vect.at(2) astype string;
mytruefalse = vect.at(3) astype bool;
mydict = vect.at(22) astype Dictionary;
vect.backReturns the last item in the Vector.Normal datatype (int, bool, string etc) or object.
You should use the astype command to ensure the datatype is returned correctly.
vect.back() astype double;
vect.back() astype GlobalDictionary;
vect.clearDeletes all the contents of a Vector. Vector will be of size 0 after it is cleared.Nonevect.clear();
vect.emptyA boolean check to see if the Vector is of size 0. Returns ‘true’ if it is, otherwise ‘false’.Booleantruefalse = vect.empty();
vect.eraseDeletes one item or a range of items from the specified index/indices. All items are then shuffled down to fill the resulting ‘hole’.Nonevect.erase(2);
or
vect.erase(2, 6);
vect.frontReturns the first item in the Vector, which is always position 0. Same as using vect.at(0).Normal datatype (int, bool, string etc) or object.
You should use the astype command to ensure the datatype is returned correctly.
vect.back() astype double;
vect.insertInserts an item one or more times into the Vector at a specific index. All items at that point in the index are shuffled up to accommodate the new items. Items cannot be inserted at the end of the Vector. To add items to the end requires ‘push_back’.Nonevect.insert(1, “bob”)
or
vect.insert(5, 3, “name”);
vect.pop_backDeletes the last item in the Vector, decreasing the size of the Vector by 1.Nonevect.pop_back();
vect.push_backInserts an item at the end of the Vector. This will increase the size of the Vector by 1.Nonevect.push_back(“steve”);
vect.push_back(26.43);

* see Common Questions & Issues section, below

Exceptions

ExceptionErrorDescription
elsystem.InvalidOperationExceptionVector is emptyUsing ‘front’ or ‘back’ on empty Vector.
elsystem.InvalidOperationExceptionVector is empty or invalid indexUsing ‘at’ to access index that doesn’t exist.
elsystem.ArgumentExceptionElement index needs to be in Vector’s rangeUsing ‘erase’ to remove an index that doesn’t exist, or using ‘insert’ to add data above the current size of the Vector.

Examples

  1. Vectors are used in the in-built TradeStation functions OpenSession, HighSession, LowSession, CloseSession, VolumeSession, VolumeDay and OHLCVCollection.

Common Questions & Issues:

1.

Accessing invalid data with ‘at’.

2.

Erasing invalid data, or inserting above the current size.

3.

Using ‘front’ or ‘back’ on empty Vector.

4. Which is faster, a Vector or an Array ?
That depends on the situation. In cases where the size of the collection needs to be dynamic, Vectors appear to be faster. In cases where access to historical values is not required, Vectors appear to be faster.

5. I’m getting unexpected results from Vector.Erase( int, int ), why ?
One of the overloaded .Erase methods for the Vector class includes the ability to erase a range of elements ( see .Erase( int, int ) in the Methods section above ). As of Build 8732, the documentation in both ‘Help – EasyLanguage Object Reference’ and the ‘Dictionary’ describe the use of this range in a way that leads to these unexpected results. 

更新于 2020年5月27日

这篇文章对您有帮助吗?

相关的话题

留言评论