老生常谈:值类型 V.S. 引用类型》中花了很大的篇幅介绍ref参数针对值类型和引用类型变量的传递。在C#中,除了方法的ref参数,我们还有很多使用ref关键字传递引用/地址的场景,本篇文章作一个简单的总结。

一、参数

二、数组索引

三、方法

四、ref 结构体

五、ref 结构体字段

一、参数

如果在方法的参数(不论是值类型和引用类型)添加了ref关键字,意味着将变量的地址作为参数传递到方法中。目标方法利用ref参数不仅可以直接操作原始的变量,还能直接替换整个变量的值。如下的代码片段定义了一个基于结构体的Record类型Foobar,并定义了Update和Replace方法,它们具有的唯一参数类型为Foobar,并且前置了ref关键字。

static void Update(ref Foobar foobar)
{
foobar.Foo = 0;
} static void Replace(ref Foobar foobar)
{
foobar = new Foobar(0, 0);
} public record struct Foobar(int Foo, int Bar);

基于ref参数针对原始变量的修改和替换体现在如下所示的演示代码中。

var foobar = new Foobar(1, 2);
Update(ref foobar);
Debug.Assert(foobar.Foo == 0);
Debug.Assert(foobar.Bar == 2); Replace(ref foobar);
Debug.Assert(foobar.Foo == 0);
Debug.Assert(foobar.Bar == 0);

C#中的ref + Type(ref Foobar)在IL中会转换成一种特殊的引用类型Type&。如下所示的是上述两个方法针对IL的声明,可以看出它们的参数类型均为Foobar&。

.method assembly hidebysig static
void '<<Main>$>g__Update|0_0' (
valuetype Foobar& foobar
) cil managed .method assembly hidebysig static
void '<<Main>$>g__Replace|0_1' (
valuetype Foobar& foobar
) cil managed

二、数组索引

我们知道数组映射一段连续的内存空间,具有相同字节长度的元素“平铺”在这段内存上。我们可以利用索引提取数组的某个元素,如果索引操作符前置了ref关键值,那么返回的就是索引自身的引用/地址。与ref参数类似,我们利用ref array[index]不仅可以修改索引指向的数组元素,还可以直接将该数组元素替换掉。

var array = new Foobar[] { new Foobar(1, 1), new Foobar(2, 2), new Foobar(3, 3) };

Update(ref array[1]);
Debug.Assert(array[1].Foo == 0);
Debug.Assert(array[1].Bar == 2); Replace(ref array[1]);
Debug.Assert(array[1].Foo == 0);
Debug.Assert(array[1].Bar == 0);

由于ref关键字在IL中被被转换成“引用类型”,所以对应的“值”也只能存储在对应引用类型的变量上,引用变量同样通过ref关键字来声明。下面的代码演示了两种不同的变量赋值,前者将Foobar数组的第一个元素的“值”赋给变量foobar(类型为Foobar),后者则将第一个元素在数组中的地址赋值给变量foobarRef(类型为Foobar&)。

var array = new Foobar[] { new Foobar(1, 1), new Foobar(2, 2), new Foobar(3, 3) };
Foobar foobar = array[0];
ref Foobar foobarRef = ref array[0]; 或者 var foobar = array[0];
ref var foobarRef = ref array[0];

上边这段C#代码将会转换成如下这段IL代码。我们不仅可以看出foobar和foobarRef声明的类型的不同(Foobar和Foobar&),还可以看到array[0]和ref array[0]使用的IL指令的差异,前者使用的是ldelem(Load Element)后者使用的是ldelema(Load Element Addess)。

.method private hidebysig static
void '<Main>$' (
string[] args
) cil managed
{
// Method begins at RVA 0x209c
// Header size: 12
// Code size: 68 (0x44)
.maxstack 5
.entrypoint
.locals init (
[0] valuetype Foobar[] 'array',
[1] valuetype Foobar foobar,
[2] valuetype Foobar& foobarRef
) // {
IL_0000: ldc.i4.3
// (no C# code)
IL_0001: newarr Foobar
IL_0006: dup
IL_0007: ldc.i4.0
// Foobar[] array = new Foobar[3]
// {
// new Foobar(1, 1),
// new Foobar(2, 2),
// new Foobar(3, 3)
// };
IL_0008: ldc.i4.1
IL_0009: ldc.i4.1
IL_000a: newobj instance void Foobar::.ctor(int32, int32)
IL_000f: stelem Foobar
IL_0014: dup
IL_0015: ldc.i4.1
IL_0016: ldc.i4.2
IL_0017: ldc.i4.2
IL_0018: newobj instance void Foobar::.ctor(int32, int32)
IL_001d: stelem Foobar
IL_0022: dup
IL_0023: ldc.i4.2
IL_0024: ldc.i4.3
IL_0025: ldc.i4.3
IL_0026: newobj instance void Foobar::.ctor(int32, int32)
IL_002b: stelem Foobar
IL_0030: stloc.0
// Foobar foobar = array[0];
IL_0031: ldloc.0
IL_0032: ldc.i4.0
IL_0033: ldelem Foobar
IL_0038: stloc.1
// ref Foobar reference = ref array[0];
IL_0039: ldloc.0
IL_003a: ldc.i4.0
IL_003b: ldelema Foobar
IL_0040: stloc.2
// (no C# code)
IL_0041: nop
// }
IL_0042: nop
IL_0043: ret
} // end of method Program::'<Main>$'

三、方法

方法可以通过前置的ref关键字返回引用/地址,比如变量或者数组元素的引用/地址。如下面的代码片段所示,方法ElementAt返回指定Foobar数组中指定索引的地址。由于该方法返回的是数组元素的地址,所以我们利用返回值直接修改对应数组元素(调用Update方法),也可以直接将整个元素替换掉(调用Replace方法)。如果我们查看ElementAt基于IL的声明,同样会发现它的返回值为Foobar&

var array = new Foobar[] { new Foobar(1, 1), new Foobar(2, 2), new Foobar(3, 3) };

var copy = ElementAt(array, 1);
Update(ref copy);
Debug.Assert(array[1].Foo == 2);
Debug.Assert(array[1].Bar == 2);
Replace(ref copy);
Debug.Assert(array[1].Foo == 2);
Debug.Assert(array[1].Bar == 2); ref var self = ref ElementAt(array, 1);
Update(ref self);
Debug.Assert(array[1].Foo == 0);
Debug.Assert(array[1].Bar == 2);
Replace(ref self);
Debug.Assert(array[1].Foo == 0);
Debug.Assert(array[1].Bar == 0); static ref Foobar ElementAt(Foobar[] array, int index) => ref array[index];

四、ref 结构体

如果在定义结构体时添加了前置的ref关键字,那么它就转变成一个ref结构体。ref结构体和常规结构最根本的区别是它不能被分配到堆上,并且总是以引用的方式使用它,永远不会出现“拷贝”的情况,最重要的ref 结构体莫过于Span<T>了。如下这个Foobar结构体就是一个包含两个数据成员的ref结构体。

public ref struct Foobar{
public int Foo { get; }
public int Bar { get; }
public Foobar(int foo, int bar)
{
Foo = foo;
Bar = bar;
}
}

ref结构体具有很多的使用约束。对于这些约束,很多人不是很理解,其实我们只需要知道这些约束最终都是为了确保:ref结构体只能存在于当前线程堆栈,而不能转移到堆上。基于这个原则,我们来具体来看看ref结构究竟有哪些使用上的限制。

1. 不能作为泛型参数

除非我们能够显式将泛型参数约束为ref结构体,对应的方法严格按照ref结构的标准来操作对应的参数或者变量,我们才能够能够将ref结构体作为泛型参数。否则对于泛型结构体,涉及的方法肯定会将其当成一个常规结构体看待,若将ref结构体指定为泛型参数类型自然是有问题。但是针对ref结构体的泛型约束目前还没有,所以我们就不能将ref结构体作为泛型参数,所以按照如下的方式创建一个Wrapper<Foobar>(Foobar为上面定义的ref结构体,下面不再单独说明)的代码是不能编译的。

// Error	CS0306	The type 'Foobar' may not be used as a type argument
var wrapper = new Wrapper<Foobar>(new Foobar(1, 2)); public class Wrapper<T>
{
public Wrapper(T value) => Value = value;
public T Value { get; }
}

2. 不能作为数组元素类型

数组是分配在堆上的,我们自然不能将ref结构体作为数组的元素类型,所以如下的代码也会遇到编译错误。

//Error	CS0611	Array elements cannot be of type 'Foobar'
var array = new Foobar[16];

3. 不能作为类型和非ref结构体数据成员

由于类的实例分配在堆上,常规结构体也并没有纯栈分配的约束,ref结构体自然不能作为它们的数据成员,所以如下所示的类和结构体的定义都是不合法的。

public class Foobarbaz
{
//Error CS8345 Field or auto-implemented property cannot be of type 'Foobar' unless it is an instance member of a ref struct.
public Foobar Foobar { get; }
public int Baz { get; }
public Foobarbaz(Foobar foobar, int baz)
{
Foobar = foobar;
Baz = baz;
}
}

或者

public structure Foobarbaz
{
//Error CS8345 Field or auto-implemented property cannot be of type 'Foobar' unless it is an instance member of a ref struct.
public Foobar Foobar { get; }
public int Baz { get; }
public Foobarbaz(Foobar foobar, int baz)
{
Foobar = foobar;
Baz = baz;
}
}

4. 不能实现接口

当我们以接口的方式使用某个结构体时会导致装箱,并最终导致对分配,所以ref结构体不能实现任意接口。

//Error    CS8343    'Foobar': ref structs cannot implement interfaces
public ref struct Foobar : IEquatable<Foobar>
{
public int Foo { get; }
public int Bar { get; }
public Foobar(int foo, int bar)
{
Foo = foo;
Bar = bar;
} public bool Equals(Foobar other) => Foo == other.Foo && Bar == other.Bar;
}

5. 不能导致装箱

所有类型都默认派生自object,所有值类型派生自ValueType类型,但是这两个类型都是引用类型(ValueType自身是引用类型),所以将ref结构体转换成object或者ValueType类型会导致装箱,是无法通过编译的。

//Error	CS0029	Cannot implicitly convert type 'Foobar' to 'object'
Object obj = new Foobar(1, 2); //Error CS0029 Cannot implicitly convert type 'Foobar' to 'System.ValueType'
ValueType value = new Foobar(1, 2);

6. 不能在委托中(或者Lambda表达式)使用

ref结构体的变量总是引用存储结构体的栈地址,所以它们只有在创建该ref结构体的方法中才有意义。一旦方法返回,堆栈帧被回收,它们自然就“消失”了。委托被认为是一个待执行的操作,我们无法约束它们必须在某方法中执行,所以委托执行的操作中不能引用ref结构体。从另一个角度来讲,一旦委托中涉及针对现有变量的引用,必然会导致“闭包”的创建,也就是会创建一个类型来对引用的变量进行封装,这自然也就违背了“不能将ref结构体作为类成员”的约束。这个约束同样应用到Lambda表达式和本地方法上。

public class Program
{
static void Main()
{
var foobar = new Foobar(1, 2);
//Error CS8175 Cannot use ref local 'foobar' inside an anonymous method, lambda expression, or query expression
Action action1 = () => Console.WriteLine(foobar); //Error CS8175 Cannot use ref local 'foobar' inside an anonymous method, lambda expression, or query expression
void Print() => Console.WriteLine(foobar);
}
}

7. 不能在async/await异步方法中

这个约束与上一个约束类似。一般来说,一个异步方法执行过程中遇到await语句就会字节返回,后续针对操作具有针对ref结构体引用,自然是不合法的。从另一方面来讲,async/await最终会转换成基于状态机的类型,依然会出现利用自动生成的类型封装引用变量的情况,同样违背了“不能将ref结构体作为类成员”的约束。

async Task InvokeAsync()
{
await Task.Yield();
//Error CS4012 Parameters or locals of type 'Foobar' cannot be declared in async methods or async lambda
var foobar = new Foobar(1, 2);
}

值得一提的是,对于返回类型为Task的异步方法,如果没有使用async关键字,由于它就是一个普通的方法,编译器并不会执行基于状态机的代码生成,所以可以自由地使用ref结构体。

public Task InvokeAsync()
{
var foobar = new Foobar(1, 2);
...
return Task.CompletedTask;
}

8. 不能在迭代器中使用

如果在一个返回IEnumerable<T>的方法中使用了yield return语句作为集合元素迭代器(interator),意味着涉及的操作执行会“延迟”到作为返回对象的集合被真正迭代(比如执行foreach语句)的时候,这个时候原始方法的堆栈帧已经被回收。

IEnumerable<(int Foo, int Bar)> Deconstruct(Foobar foobar1, Foobar foobar2)
{
//Error CS4013 Instance of type 'Foobar' cannot be used inside a nested function, query expression, iterator block or async method
yield return (foobar1.Foo, foobar1.Bar);
//Error CS4013 Instance of type 'Foobar' cannot be used inside a nested function, query expression, iterator block or async method
yield return (foobar2.Foo, foobar2.Bar);
}

9. readonly ref 结构体

顺表补充一下,我们可以按照如下的方式添加前置的readonly关键字定义一个只读的ref结构体。对于这样的结构体,其数据成员只能在被构造或者被初始化的时候进行指定,所以只能定义成如下的形式。

public readonly ref struct Foobar{
public int Foo { get; }
public int Bar { get; }
public Foobar(int foo, int bar)
{
Foo = foo;
Bar = bar;
}
}
public readonly ref struct Foobar
{
public int Foo { get; init; }
public int Bar { get; init; }
}
public readonly ref struct Foobar
{
public readonly int Foo;
public readonly int Bar;
public Foobar(int foo, int bar)
{
Foo = foo;
Bar = bar;
}
}

如果为属性定义了set方法,或者其字段没有设置成“只读”,这样的readonly ref 结构体均是不合法的。

public readonly ref struct Foobar
{
//Error CS8341 Auto-implemented instance properties in readonly structs must be readonly.
public int Foo { get; set; }
//Error CS8341 Auto-implemented instance properties in readonly structs must be readonly.
public int Bar { get; set; }
}
public readonly ref struct Foobar
{
//Error CS8340 Instance fields of readonly structs must be readonly.
public int Foo;
//Error CS8340 Instance fields of readonly structs must be readonly.
public int Bar;
}

五、ref 结构体字段

我们可以在ref结构体的字段成员前添加ref关键字使之返回一个引用。除此之外,我们还可以进一步添加readonly关键字创建“只读引用字段”,并且这个readonly关键可以放在ref后面(ref readonly),也可以放在ref前面(readonly ref),还可以前后都放(readonly ref readonly)。如果你之前没有接触过ref字段,是不是会感到很晕?希望一下的内容能够为你解惑。上面的代码片段定义了一个名为RefStruct的ref 结构体,定义其中的四个字段(Foo、Bar、Baz和Qux)都是返回引用的ref 字段。除了Foo字段具有具有可读写的特性外,我们采用上述三种不同的形式将其余三个字段定义成“自读”的。

public ref struct RefStruct
{
public ref KV Foo;
public ref readonly KV Bar;
public readonly ref KV Baz;
public readonly ref readonly KV Qux;
public RefStruct(ref KV foo, ref KV bar, ref KV baz, ref KV qux)
{
Foo = ref foo;
Bar = ref bar;
Baz = ref baz;
Qux = ref qux;
}
} public struct KV
{
public int Key;
public int Value;
public KV(int key, int value)
{
Key = key;
Value = value;
}
}

1. Writable

在如下的演示代码中,我们针对同一个KV对象的引用创建了RefStruct。在直接修改Foo字段返回的KV之后,由于四个字段引用的都是同一个KV,所以其余三个字段都被修改了。由于Foo字段是可读可写的,所以当我们为它指定一个新的KV后,其他三个字段也被替换了。

KV kv = default;

var value = new RefStruct(ref kv, ref kv, ref kv, ref kv);
value.Foo.Key = 1;
value.Foo.Value = 1;
Debug.Assert(kv.Key == 1);
Debug.Assert(kv.Value == 1);
Debug.Assert(value.Foo.Key == 1);
Debug.Assert(value.Foo.Value == 1);
Debug.Assert(value.Bar.Key == 1);
Debug.Assert(value.Bar.Value == 1);
Debug.Assert(value.Baz.Key == 1);
Debug.Assert(value.Baz.Value == 1);
Debug.Assert(value.Qux.Key == 1);
Debug.Assert(value.Qux.Value == 1); value.Foo = new KV(2, 2);
Debug.Assert(kv.Key == 2);
Debug.Assert(kv.Value == 2);
Debug.Assert(value.Foo.Key == 2);
Debug.Assert(value.Foo.Value == 2);
Debug.Assert(value.Bar.Key == 2);
Debug.Assert(value.Bar.Value == 2);
Debug.Assert(value.Baz.Key == 2);
Debug.Assert(value.Baz.Value == 2);
Debug.Assert(value.Qux.Key == 2);
Debug.Assert(value.Qux.Value == 2);

2. ref readonly

第一个字段被定义成“ref readonly”,readonly被置于ref之后,表示readonly并不是用来修饰ref,而是用来修饰引用指向的KV对象,它使我们不能修改KV对象的数据成员。所以如下的代码是不能通过编译的。

KV kv = default;

var value = new RefStruct(ref kv, ref kv, ref kv, ref kv);
//Error CS8332 Cannot assign to a member of field 'Bar' or use it as the right hand side of a ref assignment because it is a readonly variable
value.Bar.Key = 2;
//Error CS8332 Cannot assign to a member of field 'Bar' or use it as the right hand side of a ref assignment because it is a readonly variable
value.Bar.Value = 2;

但是这仅仅能够保证我们不能直接通过字段进行修改而已,我们依然可以通过将字段赋值给另一个变量,利用这个变量依然达到更新该字段的目的。

KV kv = default;
var value = new RefStruct(ref kv, ref kv, ref kv, ref kv); kv = value.Bar;
kv.Key = 1;
kv.Value = 1;
Debug.Assert(value.Baz.Key == 1);
Debug.Assert(value.Baz.Value == 1);

由于readonly并不是修饰引用本身,所以我们采用如下的方式通过修改引用达到替换字段的目的。

KV kv = default;
KV another = new KV(1,1); var value = new RefStruct(ref kv, ref kv, ref kv, ref kv);
value.Bar = ref another;
Debug.Assert(value.Bar.Key == 1);
Debug.Assert(value.Bar.Key == 1);

3. readonly ref

如果readonly被置于ref前面,就意味着引用本身,所以针对Baz字段的赋值是不合法的。

KV kv = default;

var value = new RefStruct(ref kv, ref kv, ref kv, ref kv);
KV another = new KV(1, 1);
//Error CS0191 A readonly field cannot be assigned to (except in a constructor or init-only setter of the type in which the field is defined or a variable initializer)
value.Baz = ref another;

但是引用指向的KV对象是可以直接通过字段进行修改。

KV kv = default;
var value = new RefStruct(ref kv, ref kv, ref kv, ref kv); value.Baz.Key = 1;
value.Baz.Value = 1;
Debug.Assert(value.Baz.Key == 1);
Debug.Assert(value.Baz.Key == 1);

4. readonly ref readonly

现在我们知道了ref前后的readonly分别修饰的是字段返回的引用和引用指向的目标对象,所以对于readonly ref readonly修饰的字段Qux,我们既不能字节将其替换成指向另一个KV的引用,也不能直接利用它修改该字段指向的KV对象。

KV kv = default;
var another = new KV(1, 1);
var value = new RefStruct(ref kv, ref kv, ref kv, ref kv);
//Error CS0191 A readonly field cannot be assigned to (except in a constructor or init-only setter of the type in which the field is defined or a variable initializer)
value.Qux = ref another;
KV kv = default;
var value = new RefStruct(ref kv, ref kv, ref kv, ref kv);
//Error CS8332 Cannot assign to a member of field 'Qux' or use it as the right hand side of a ref assignment because it is a readonly variable
value.Qux.Key = 1;
//Error CS8332 Cannot assign to a member of field 'Qux' or use it as the right hand side of a ref assignment because it is a readonly variable
value.Qux.Value = 1;

除了参数,ref关键字还可以用在什么地方?的更多相关文章

  1. C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处

    ​ 一直以为对于引用类型做为参数在方法调用时加不加 ref 关键字是没有区别的.但是今天一调试踪了一下变量内存情况才发现大有不同. 直接上代码,结论是:以下代码是使用了 ref 关键字的版本.它输出1 ...

  2. C# 方法参数传递方式 关键字(in、out、ref)

    in:  值传递,默认传递方式: ref:地址/引用传递,调用时该参数必需已经初始化: out:地址/引用传递,调用时该参数不需要先初始化(被调用方负责该参数的初始化). 注1: in  关键字用于向 ...

  3. c# ref关键字对于引用类型传递的影响

    我们可能见到下面的代码 public static void StringBuilderNoRef(StringBuilder s)     { s.Append(" World" ...

  4. 浅谈c#的三个高级参数ref out 和Params

    c#的三个高级参数ref out 和Params 前言:在我们学习c#基础的时候,我们会学习到c#的三个高级的参数,分别是out .ref 和Params,在这里我们来分别的讲解一下,在这里的我们先不 ...

  5. C#返回多个参数 ref及out

    out 关键字会导致参数通过引用来传递.这与 ref 关键字类似,不同之处在于 ref 要求变量必须在传递之前进行初始化.若要使用 out 参数,方法定义和调用方法都必须显式使用 out 关键字.例如 ...

  6. 浅谈c#的三个高级参数ref out 和Params C#中is与as的区别分析 “登陆”与“登录”有何区别 经典SQL语句大全(绝对的经典)

    浅谈c#的三个高级参数ref out 和Params   c#的三个高级参数ref out 和Params 前言:在我们学习c#基础的时候,我们会学习到c#的三个高级的参数,分别是out .ref 和 ...

  7. C#中ref关键字的用法总结

    ref表示引用的意思,C#中它有多种用法,这里简单总结一下: 1.按引用传递参数 具体可见:C#中的值传递与引用传递(in.out.ref) 2.引用局部变量 引用局部变量指的是在变量声明时使用ref ...

  8. C#基础:值类型、引用类型与ref关键字

    在C#中,ref的意思是按引用传递.可以参考C++: int a = 10, b = 20; void swap(int x, int y) { int temp = x; x = y; y = te ...

  9. ref关键字的作用

    ref关键字使参数按引用传递.其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中. 就是说,在调用方法的参数中使用ref关键字可以使得变量能够改变. ref和out都是 ...

  10. ref 关键字out关键字

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

随机推荐

  1. Python集合常用操作

    一.集合的介绍   1.定义:集合是无序的,集合中的元素是唯一的,集合一般用于元组或者列表中的元素去重.   2.特性:集合的目的是将不同的值存放在一起,不同的集合间用来做关系运算,无须纠结于集合中的 ...

  2. pysimplegui之元素常用属性

    常用元素参数 您将在几乎所有元素创建调用中看到的一些参数包括: key - 与 window[key].事件和返回值字典一起使用 工具提示tooltip - 将鼠标悬停在元素上,您将获得包含此文本的弹 ...

  3. c#快速入门~在java基础上,知道C#和JAVA 的不同即可

    观看下文前提:如果你的主语言是java,现在想再学一门新语言C#,下文是在java基础上,对比和java的不同,快速上手C# C# 学习参考文档和开发工具 微软c#官方文档:https://learn ...

  4. 全网最详细中英文ChatGPT-GPT-4示例文档-食谱智能生成从0到1快速入门——官网推荐的48种最佳应用场景(附python/node.js/curl命令源代码,小白也能学)

    目录 Introduce 简介 setting 设置 Prompt 提示 Sample response 回复样本 API request 接口请求 python接口请求示例 node.js接口请求示 ...

  5. 21-HMR

    /* HMR:hot module replacement 热模块替换 / 模块热替换 作用:一个模块发生变化,只会重新打包这一个模块(而不是打包所有模块) 极大提升构建速度 样式文件:可以使用HMR ...

  6. 推荐一个.Ner Core开发的配置中心开源项目

    当你把单体应用改造为微服务架构,相应的配置文件,也会被分割,被分散到各个节点.这个时候就会产生一个问题,配置信息是分散的.冗余的,变成不好维护管理.这个时候我们就需要把配置信息独立出来,成立一个配置中 ...

  7. java线程池和多线程的使用详解

    Java 多线程和线程池使用 java多线程实现的几种方法 1.继承Thread类 继承Thread类,重写run方法,创建线程类对象调用start方法启动线程. public class Threa ...

  8. 2021-12-14:根据身高重建队列。 假设有打乱顺序的一群人站成一个队列,数组 people 表示队列中一些人的属性(不一定按顺序)。每个 people[i] = [hi, ki] 表示第 i 个

    2021-12-14:根据身高重建队列. 假设有打乱顺序的一群人站成一个队列,数组 people 表示队列中一些人的属性(不一定按顺序).每个 people[i] = [hi, ki] 表示第 i 个 ...

  9. 2021-08-19:超级洗衣机。假设有 n 台超级洗衣机放在同一排上。开始的时候,每台洗衣机内可能有一定量的衣服,也可能是空的。在每一步操作中,你可以选择任意 m (1 ≤ m ≤ n) 台洗衣机,

    2021-08-19:超级洗衣机.假设有 n 台超级洗衣机放在同一排上.开始的时候,每台洗衣机内可能有一定量的衣服,也可能是空的.在每一步操作中,你可以选择任意 m (1 ≤ m ≤ n) 台洗衣机, ...

  10. ICLR 2018-A Simple Neural Attentive Meta-Learner

    Key 时序卷积+注意力机制(前者从过去的经验中收集信息,而后者则精确定位具体的信息.) 解决的主要问题 手工设计的限制:最近的许多元学习方法都是大量手工设计的,要么使用专门用于特定应用程序的架构,要 ...