Platform::Collections::Vector 类

表示可按照索引单独访问的对象的有序集合。 实现 Windows::Foundation::Collections::IObservableVector 以帮助进行 XAML 数据绑定

语法

template <typename T, typename E>
   ref class Vector sealed;

参数

T
向量对象中包含的元素的类型。

E
使用 T 类型的值指定测试相等性的二进制谓词。默认值为 std::equal_to<T>

注解

允许的类型是:

  1. integers

  2. interface class^

  3. 公共 ref 类

  4. value struct

  5. 公共枚举类

Vector 类是 Windows::Foundation::Collections::IVector 接口的 C++ 具体实现。

如果你尝试在公共返回值或参数中使用 Vector 类型,则会引发编译器错误 C3986。 通过将参数或返回值的类型更改为 Windows::Foundation::Collections::IVector可修复该错误。 有关更多信息,请参见 集合 (C++/CX)

成员

公共构造函数

名称 描述
Vector::Vector 初始化 Vector 类的新实例。

公共方法

名称 描述
Vector::Append 在当前向量中的最后一项后插入指定项。
Vector::Clear 删除当前向量中的所有元素。
Vector::First 返回指定该向量中第一个元素的迭代器。
Vector::GetAt 检索由指定索引标识的当前向量的元素。
Vector::GetMany 从指定索引处开始,检索当前向量中的项目序列。
Vector::GetView 返回向量的只读视图,即 Platform::Collections::VectorView
Vector::IndexOf 在当前向量中搜索指定项,如果找到,则返回该项的索引。
Vector::InsertAt 在当前 Vector 中由指定的索引标识的元素处插入指定的项。
Vector::ReplaceAll 删除当前向量中的元素,然后插入来自指定数组的元素。
Vector::RemoveAt 从当前向量删除指定索引标识的元素。
Vector::RemoveAtEnd 删除当前矢量末尾的元素。
Vector::SetAt 将指定值分配给当前向量中指定索引标识的元素。
Vector::Size 返回当前向量对象中的元素数目。

事件

名称 描述
事件 Windows::Foundation::Collection::VectorChangedEventHandler<T>^ VectorChanged 当向量更改时发生。

继承层次结构

Vector

要求

标头: collection.h

命名空间: Platform::Collections

Vector::Append 方法

在当前向量中的最后一项后插入指定项。

语法

virtual void Append(T item);

参数

index
要插入到向量中的项。 的类型由 T 类型名称定义。

Vector::Clear 方法

删除当前向量中的所有元素。

语法

virtual void Clear();

Vector::First 方法

返回指向该向量中第一个元素的迭代器。

语法

virtual Windows::Foundation::Collections::IIterator <T>^ First();

返回值

指向该向量中第一个元素的迭代器。

注解

保留 First() 返回的迭代器的一种简便方式是将返回值分配给用 auto 类型推导关键字声明的变量。 例如 auto x = myVector->First();。 此迭代器知道该集合的长度。

当你需要一对要传递到 STL 函数的迭代器时,请使用自由格式函数 Windows::Foundation::Collections::beginWindows::Foundation::Collections::end

Vector::GetAt 方法

检索由指定索引标识的当前向量的元素。

语法

virtual T GetAt(unsigned int index);

参数

index
从零开始的无符号整数,用于指定 Vector 对象中的特定元素。

返回值

索引参数指定的元素。 元素类型通过 T 类型名称定义。

Vector::GetMany 方法

检索当前向量中的项序列,从指定的索引开始,并将它们复制到调用方分配的数组中。

语法

virtual unsigned int GetMany(
    unsigned int startIndex,
    Platform::WriteOnlyArray<T>^ dest);

参数

startIndex
要检索的项开头从零开始的索引。

dest
调用方分配的项数组,以由 startIndex 指定的元素开始,以向量中最后一个元素结尾。

返回值

已检索的项的数量。

备注

此函数并非旨在由客户端代码直接使用。 在 to_vector 函数中内部使用它以支持 Platform::Vector 实例到 std::vector 实例的高效转换。

Vector::GetView 方法

返回向量的只读视图,即 IVectorView。

语法

Windows::Foundation::Collections::IVectorView<T>^ GetView();

返回值

一个 IVectorView 对象。

Vector::IndexOf 方法

在当前向量中搜索指定项,如果找到,则返回该项的索引。

语法

virtual bool IndexOf(T value, unsigned int* index);

参数

value
要查找的项。

索引
如果找到参数,则为该项的从零开始的索引;否则,为 0。

如果该项目是 Vector 的第一个元素,或者未找到该项目,则索引参数为 0。 如果返回值为 true,则表明找到该项目,并且它是第一个元素;否则,表示未找到该项目。

返回值

如果找到指定项目,则为 true;否则,为 false

注解

IndexOf 使用 std::find_if 查找该项目。 因此,自定义元素类型应该重载 == 和 != 运算符以支持 find_if 所需的相等性比较。

Vector::InsertAt 方法

在当前 Vector 中由指定的索引标识的元素处插入指定的项。

语法

virtual void InsertAt(unsigned int index, T item)

参数

index
从零开始的无符号整数,用于指定 Vector 对象中的特定元素。

item
要插入到 Vector 中由索引指定的元素处的项。 的类型由 T 类型名称定义。

Vector::RemoveAt 方法

从当前向量删除指定索引标识的元素。

语法

virtual void RemoveAt(unsigned int index);

参数

index
从零开始的无符号整数,用于指定 Vector 对象中的特定元素。

Vector::RemoveAtEnd 方法

删除当前矢量末尾的元素。

语法

virtual void RemoveAtEnd();

Vector::ReplaceAll 方法

删除当前向量中的元素,然后插入来自指定数组的元素。

语法

virtual void ReplaceAll(const ::Platform::Array<T>^ arr);

参数

arr
T 类型名称定义类型的对象的数组。

Vector::SetAt 方法

将指定值分配给当前向量中指定索引标识的元素。

语法

virtual void SetAt(unsigned int index, T item);

参数

index
从零开始的无符号整数,用于指定 Vector 对象中的特定元素。

item
要分配给指定元素的值。 的类型由 T 类型名称定义。

Vector::Size 方法

返回当前向量对象中的元素数目。

语法

virtual property unsigned int Size;

返回值

当前 Vector 中的元素数目。

Vector::Vector 构造函数

初始化 Vector 类的新实例。

语法

Vector();

explicit Vector(unsigned int size);
Vector( unsigned int size, T value);
template <typename U> explicit Vector( const ::std::vector<U>& v);
template <typename U> explicit Vector( std::vector<U>&& v);

Vector( const T * ptr, unsigned int size);
template <size_t N> explicit Vector(const T(&arr)[N]);
template <size_t N> explicit Vector(const std::array<T, N>& a);
explicit Vector(const Array<T>^ arr);

template <typename InIt> Vector(InIt first, InIt last);
Vector(std::initializer_list<T> il);

参数

a
用于初始化 Vector 的 std::array

arr
用于初始化 Vector 的 Platform::Array

InIt
用于初始化当前向量的对象集合的类型。

il
用于初始化 Vector 的 T 类型的对象的 std::initializer_list

N
用于初始化当前向量的对象集合中的元素数。

size
向量中元素的数目。

value
用于初始化当前向量中每个元素的值。

v
对用于初始化当前 Vector 的 std::vector左值和右值

ptr
指向用于初始化当前向量的 std::vector 的指针。

first
用于初始化当前向量的对象序列中的第一个元素。 第一的类型通过完美转发传递。 有关详细信息,请参阅右值引用声明符:&&

last
用于初始化当前向量的对象序列中的最后一个元素。 最后的类型通过完美转发传递。 有关详细信息,请参阅右值引用声明符:&&

另请参阅

集合 (C++/CX)
Platform 命名空间
使用 C++ 创建 Windows 运行时组件