固态阵列是否具有弹出功能? [英] Is there a pop functionality for solidity arrays?

查看:62
本文介绍了固态阵列是否具有弹出功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经利用可靠性将数据推入数组。

I have used solidity to push data into an array. Is there a similar function for pop ?

string[] myArray;
myArray.push("hello")

最佳解决方案是什么?如何牢固地删除动态数组中的元素?

What is the best solution for this ? How do I delete an element in a dynamic array in solidity ?

推荐答案

更新2-19-2019 :正如Joel在下面指出的那样, pop 已添加到内置数组支持中。请参阅 https://solidity.readthedocs.io/en/v0 .5.4 / types.html#array-members 。如果其他人正在使用较旧版本的Solidity,请在此处保留原始答案。

Update 2-19-2019: As Joel pointed out below, pop has been added to built-in array support. See https://solidity.readthedocs.io/en/v0.5.4/types.html#array-members. Leaving original answer on here in case others are using older versions of Solidity.

Solidity中没有弹出功能。您可以考虑使用一些方法来维护阵列。

There is no pop function in Solidity. You have a few options you can consider for maintaining your array.

删除并删除数组。留空

最简单的解决方案是仅删除特定索引处的元素:

The simplest solution is to just delete the element at a specific index:

string element = myArray[index];
delete myArray[index];
return element;

但是,这不会移动数组中的元素,并且会保留元素 string 0在您的数组中。要检查此元素,您将使用

However, this will NOT shift the elements in your array and will leave an element of "string 0" in your array. To check this element, you would use

if(bytes(myArray [index])。length> 0)...

交换&删除

如果您不关心数组中的顺序,则可以将元素与数组中的最后一个元素交换,然后删除:

If you don't care about order in your array, you can swap the element with the last element in your array and then delete:

string element = myArray[index];
myArray[index] = myArray[myArray.length - 1];
delete myArray[myArray.length - 1];
myArray.length--;
return element;

带班次删除

如果数组中的顺序很重要,则可以删除该元素,然后将所有剩余元素移到左侧。

If order in your array is important, you can delete the element then shift all remaining elements to the left.

string element = myArray[index];
for (uint i = index; i < myArray.length - 1; i++) {
  myArray[index] = myArray[index + 1];
}
delete myArray[myArray.length - 1];
myArray.length--;
return element;

请注意,这将是最昂贵的选择。如果阵列很长,则天然气使用量很高。

Note that this will be the most expensive of the options. If your array is very long, you will have high gas usage.

与@Jedsada的建议相关,这里是一个库版本:

Correlating with @Jedsada's suggestion, here is a version as a library:

pragma solidity ^0.4.24;

library StackLib {
  using StackLib for Stack;

  struct Stack {
    uint[] _items;
  }

  function pushElement(Stack storage self, uint element) internal returns (bool) {
    self._items.push(element);
  }

  function popElement(Stack storage self) internal returns (uint) {
    uint element = self.peek();

    if (self.size() > 0)
      delete self._items[self.size() - 1];

    return element;
  }

  function peek(Stack storage self) internal returns (uint) {
    uint value;

    if (self.size() > 0)
      value = self._items[self.size() - 1];

    return value;
  }

  function size(Stack storage self) internal returns (uint8) {
    return self.size();
  }
}

用法示例(重要说明:您不能使用 popElement 并将值返回给客户端。该方法更改状态,只应在事务中使用。)

Example usage (Important note: You can't use popElement and return the value to a client. That method changes state and should only be used within a transaction.):

contract Test {
  using StackLib for StackLib.Stack;

  StackLib.Stack numbers;

  function add(uint v) public {
    numbers.pushElement(v);
  }

  function doSomething() public {
    for (uint8 i = 0; i < numbers.size(); i++) {
      uint curNum = numbers.popElement();

      // do something with curNum
    }
  }
}

附加说明:不幸的是, var 自0.4.20开始已被弃用,并且不能替代泛型。您必须自定义特定类型。

Additional note: Unfortunately, var has been deprecated since 0.4.20 and there is no replacement for generics. You have to customize for a specific type.

这篇关于固态阵列是否具有弹出功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆