详解Rust标准库:VecDeque 队列

时间:2024-11-07 07:08:27

theme: github
highlight: an-old-hope

查看本地官方文档

安装rust后运行

rustup doc

查看The Standard Library即可获取标准库内容

std::connections::VecDeque定义

队列是遵循先入先出规则的线性数据结构,在内存中不一定连续

VecDeque定义:可增长的环形缓冲区实现的双端队列

pub struct VecDeque<T, A: Allocator = Global> {
    // 队列头部在内部缓冲区中的索引位置
    head: usize,
    // 当前队列中的元素数量
    len: usize,
    // 管理内存分配和释放,确保有足够的空间来存储队列中的元素
    buf: RawVec<T, A>,
}

方法

with_capacity:用于创建一个具有指定初始容量的VecDeque

use std::collections::VecDeque;

fn main() {
    let deque = VecDeque::from([1, 2, 3]);
    if let Some(element) = deque.get(1) {
        println!("Element at index 1: {}", element);
        // Element at index 1: 2
    }
}

get:获取指定索引位置的元素的不可变引用

use std::collections::VecDeque;

fn main() {
    let deque = VecDeque::from([1, 2, 3]);
    if let Some(element) = deque.get(1) {
        println!("Element at index 1: {}", element);
        // Element at index 1: 2
    }
}

get_mut:获取指定索引位置的元素的可变引用

use std::collections::VecDeque;

fn main() {
    let mut deque = VecDeque::from([1, 2, 3]);
    if let Some(element) = deque.get_mut(1) {
        *element = 4;
    }
    println!("Modified deque: {:?}", deque);
    // Modified deque: [1, 4, 3]
}

swap:交换VecDeque中两个指定索引位置的元素

use std::collections::VecDeque;

fn main() {
    let mut deque = VecDeque::from([1, 2, 3]);
    deque.swap(0, 2);
    println!("Swapped deque: {:?}", deque);
    // Swapped deque: [3, 2, 1]
}

capacity:返回VecDeque当前的容量

use std::collections::VecDeque;

fn main() {
    let mut deque = VecDeque::with_capacity(5);
    println!("Initial capacity: {}", deque.capacity());
    // Initial capacity: 5

    deque.push_back(1);
    println!("Capacity after adding an element: {}", deque.capacity());
    // Capacity after adding an element: 5
}

reserve_exact:精确地预留足够的空间以容纳指定数量的额外元素

use std::collections::VecDeque;

fn main() {
    let mut deque = VecDeque::new();
    deque.reserve_exact(3);
    println!("Capacity after reserve_exact: {}", deque.capacity());
    // Capacity after reserve_exact: 3
    deque.push_back(1);
    deque.push_back(2);
    deque.push_back(3);
}

reserve:预留足够的空间以容纳指定数量的额外元素(空间不够自动增长)

use std::collections::VecDeque;

fn main() {
    let mut deque = VecDeque::new();
    deque.reserve(5);
    println!("Capacity after reserve: {}", deque.capacity());
    // Capacity after reserve: 5
    deque.push_back(1);
    deque.push_back(2);
    deque.push_back(3);
    deque.push_back(4);
    deque.push_back(5);
    deque.push_back(6);
    println!("Capacity after reserve: {}", deque.capacity());
    // Capacity after reserve: 10
}

try_reserve_exact:尝试精确地预留足够的空间以容纳指定数量的额外元素,如果预留成功则返回Ok(()),否则返回错误

use std::collections::VecDeque;

fn main() {
    let mut deque = VecDeque::new();
    if let Ok(()) = deque.try_reserve_exact(3) {
        println!("Reserved successfully. Capacity: {}", deque.capacity());
        // Reserved successfully. Capacity: 3
    } else {
        println!("Reserve failed.");
    }
    deque.push_back(1);
    deque.push_back(2);
    deque.push_back(3);
    deque.push_back(4);
    println!("Reserved successfully. Capacity: {}", deque.capacity());
    // Reserved successfully. Capacity: 6
}

try_reserve:尝试预留足够的空间以容纳指定数量的额外元素,如果预留成功则返回Ok(()),否则返回错误

use std::collections::VecDeque;

fn main() {
    let mut deque = VecDeque::new();
    if let Ok(()) = deque.try_reserve(3) {
        println!("Reserved successfully. Capacity: {}", deque.capacity());
        // Reserved successfully. Capacity: 4
    } else {
        println!("Reserve failed.");
    }
    deque.push_back(1);
    deque.push_back(2);
    deque.push_back(3);
    deque.push_back(4);
}

shrink_to_fit:将VecDeque的容量收缩到与当前元素数量相同

use std::collections::VecDeque;

fn main() {
    let mut deque = VecDeque::with_capacity(10);
    deque.push_back(1);
    deque.push_back(2);
    deque.push_back(3);
    deque.shrink_to_fit();
    println!("Capacity after shrink_to_fit: {}", deque.capacity());
    // Capacity after shrink_to_fit: 3
}

shrink_to:将VecDeque的容量收缩到指定大小

use std::collections::VecDeque;

fn main() {
    let mut deque = VecDeque::with_capacity(10);
    deque.push_back(1);
    deque.push_back(2);
    deque.push_back(3);
    deque.shrink_to(5);
    println!("Capacity after shrink_to: {}", deque.capacity());
    // Capacity after shrink_to: 5
}

truncate:将VecDeque截断到指定长度

use std::collections::VecDeque;

fn main() {
    let mut deque = VecDeque::from([1, 2, 3, 4, 5]);
    deque.truncate(3);
    println!("Truncated deque: {:?}", deque);
    // Truncated deque: [1, 2, 3]
}

iter:返回一个不可变的迭代器,用于遍历VecDeque中的元素

use std::collections::VecDeque;

fn main() {
    let deque = VecDeque::from([1, 2, 3]);
    for element in deque.iter() {
        println!("Element: {}", element);
    }
    // Element: 1
    // Element: 2
    // Element: 3
}

iter_mut:返回一个可变的迭代器,允许在遍历过程中修改VecDeque中的元素

use std::collections::VecDeque;

fn main() {
    let mut deque = VecDeque::from([1, 2, 3]);
    for element in deque.iter_mut() {
        *element *= 2;
    }
    println!("Modified deque: {:?}", deque);
    // Modified deque: [2, 4, 6]
}

as_slice:将VecDeque分成不可变的切片

use std::collections::VecDeque;

fn main() {
    let deque = VecDeque::from([1, 2, 3, 4, 5]);
    let (front_slice, back_slice) = deque.as_slices();
    println!("Front slice: {:?}", front_slice);
    // Front slice: [1, 2, 3, 4, 5]
    println!("Back slice: {:?}", back_slice);
    // Back slice: []
}

as_mut_slices:将VecDeque分成可变的切片

use std::collections::VecDeque;

fn main() {
    let mut deque = VecDeque::from([1, 2, 3, 4, 5]);
    let (front_slice, back_slice) = deque.as_mut_slices();
    for element in front_slice {
        *element *= 2;
    }
    for element in back_slice {
        *element *= 3;
    }
    println!("Modified deque: {:?}", deque);
    // Modified deque: [2, 4, 6, 8, 10]
}

len:返回VecDeque中元素的数量

use std::collections::VecDeque;

fn main() {
    let deque = VecDeque::from([1, 2, 3]);
    println!("Length of deque: {}", deque.len());
    // Length of deque: 3
}

is_empty:判断VecDeque是否为空

use std::collections::VecDeque;

fn main() {
    let deque:VecDeque<i32> = VecDeque::new();
    println!("Is deque empty? {}", deque.is_empty());
    // Is deque empty? true
    let deque2 = VecDeque::from([1]);
    println!("Is deque2 empty? {}", deque2.is_empty());
    // Is deque2 empty? false
}

range:返回一个指定范围内的不可变迭代器

use std::collections::VecDeque;

fn main() {
    let deque = VecDeque::from([1, 2, 3, 4, 5]);
    for element in deque.range(1..3) {
        println!("Element: {}", element);
        // Element: 2
        // Element: 3
    }
}

range_mut:返回一个指定范围内的可变迭代器

use std::collections::VecDeque;

fn main() {
    let mut deque = VecDeque::from([1, 2, 3, 4, 5]);
    for element in deque.range_mut(1..3) {
        *element *= 2;
    }
    println!("Modified deque: {:?}", deque);
    // Modified deque: [1, 4, 6, 4, 5]
}

drain:移除并返回一个迭代器,用于遍历被移除的元素

use std::collections::VecDeque;

fn main() {
    let mut deque = VecDeque::from([1, 2, 3, 4, 5]);
    let drained = deque.drain(1..3).collect::<Vec<i32>>();
    println!("Drained elements: {:?}", drained);
    // Drained elements: [2, 3]
    println!("Remaining deque: {:?}", deque);
    // Remaining deque: [1, 4, 5]
}

clear:清空VecDeque,移除所有元素

use std::collections::VecDeque;

fn main() {
    let mut deque = VecDeque::from([1, 2, 3]);
    println!("Before clearing: {:?}", deque);
    // Before clearing: [1, 2, 3]
    deque.clear();
    println!("After clearing: {:?}", deque);
    // After clearing: []
}

contains:判断VecDeque中是否包含特定元素

use std::collections::VecDeque;

fn main() {
    let deque = VecDeque::from([1, 2, 3]);
    println!("Does deque contain 2? {}", deque.contains(&2));
    // Does deque contain 2? true
    println!("Does deque contain 4? {}", deque.contains(&4));
    // Does deque contain 4? false
}

front:返回VecDeque头部元素的不可变引用,如果为空则返回None

use std::collections::VecDeque;

fn main() {
    let deque = VecDeque::from([1, 2, 3]);
    if let Some(element) = deque.front() {
        println!("Front element: {}", element);
        // Front element: 1
    }
}

front_mut:返回VecDeque头部元素的可变引用,如果为空则返回None

use std::collections::VecDeque;

fn main() {
    let mut deque = VecDeque::from([1, 2, 3]);
    if let Some(element) = deque.front_mut() {
        *element = 4;
    }
    println!("Modifi

相关文章