System::ArraySegment 类

ArraySegment class

表示一维数组的一个段。此类型应在栈上分配,并通过值或引用传递给函数。切勿使用 System::SmartPtr 类来管理此类型的对象。

template<typename T>class ArraySegment : public System::Object
参数描述
T数组段元素的类型。

方法

方法描述
ArraySegment(System::ArrayPtr<T>)
ArraySegment(System::ArrayPtr<T>, int32_t, int32_t)
ArraySegment()
Equals(System::SharedPtr<Object>) override
Equals(ArraySegment<T>)
get_Array()
get_Count()
get_Offset()
GetHashCode() const override相当于 C# 的 Object.GetHashCode() 方法。支持对自定义对象进行哈希。

备注

#include <system/array_segment.h>
#include <system/smart_ptr.h>

using namespace System;

void Print(const SmartPtr<ArraySegment<String>> &segment)
{
  for (auto i = segment->get_Offset(); i < segment->get_Offset() + segment->get_Count(); i++)
  {
    std::cout << segment->get_Array()[i] << ' ';
  }
  std::cout << std::endl;
}

int main()
{
  // 创建并填充数组。
  auto array = System::MakeObject<Array<String>>(3);
  array[0] = u"First";
  array[1] = u"Second";
  array[2] = u"Third";

  // 创建包含整个数组的数组段。
  auto fullArray = MakeObject<ArraySegment<String>>(array);

  // 打印数组段的项目。
  Print(fullArray);

  // 创建数组段。
  auto segment = MakeObject<ArraySegment<String>>(array, 1, 2);

  // 打印数组段的项目。
  Print(segment);

  return 0;
}
/*
This code example produces the following output:
First Second Third
Second Third
*/

另见