impl push(self , item : T) 用于具有 2 Vecs<T>

问题描述

我一直在尝试实现这个结构的推送:

struct StackMin<T: std::cmp::Ord>
{
    stack : Vec<T>,min : Vec<T>
}

像这样:

fn push(&mut self,item: T) {
    let l = self.stack.len();
    let x: T;
    match l {
        0 => println!("There is nothing in the stack."),n => {
            if item <= self.stack[l - 1] {
                self.stack.push(item); //item moved here
                self.min.push(item); // so I can't use it again here
            } else {
                self.stack.push(item);
            }
        }
    }
}

问题是项目随着第一个 Vec<T>::push 移动,所以我不能在第二次调用 push() 时立即使用它。我想创建一个变量 let a = &item 并在第二次调用中使用它,但推送需要“T”而不是“&T”。

此外,如果我尝试执行 a=self.stack[l-1],则会出错,因为 T 类型没有 copy/Clone 特征。

后期编辑:我还需要打印最小向量的最后一个值。但它没有 std::fmt::display ,我认为它不可能是 impl!?有什么想法吗?

你会如何处理这个问题?

解决方法

假设您可以更改结构 StackMin 的内部值,但不能更改特征要求,您可以执行以下操作:

struct MinStack<T: std::cmp::Ord> {
    // T is the data you want to store
    // and usize points to the smallest T
    inner: Vec<(T,usize)>
}

impl<T: std::cmp::Ord> MinStack<T> {
    fn push(&mut self,val: T) {
        let min_index = self.inner.last()
            // get last min value and its index
            .map(|(_,index)| (&self.inner[*index].0,index))
            // check if it is smaller then the current value
            .and_then(|(prev_min,min_index)| 
                (prev_min < &val).then(|| *min_index)
            )
            // if not smaller or does not exist
            // set it to the current index
            .unwrap_or(self.inner.len());

        self.inner.push((val,min_index));
    }
}

这是 MinStack 挑战 Rust Playground 的完整实现。
让我知道我是否应该在上面的代码中澄清一些内容。

所用方法的文档: