获取存储在n维向量中的元素数

时间:2018-08-27 14:58:38

标签: vector rust

我有一个二维向量:

let vec2d = vec![
    vec![1, 1, 1],
    vec![1, 1, 1],
];

我可以得出以这种方式存储的元素总数:

let mut n_vec_element: i32 = 0;

for i in vec2d.iter() {
    n_vec_element += i.len() as i32;
}

println!("2D vector elements :{}", n_vec_element); // prints 6

当我增加尺寸时,循环会变长:

let mut n_vec_element: i32 = 0;

let vec3d = vec![
    vec![
        vec![1, 3, 5 as i32],
        vec![2, 4, 6 as i32],
        vec![3, 5, 7 as i32],
    ],
    vec![
        vec![1, 3, 5 as i32],
        vec![2, 4, 6 as i32],
        vec![3, 5, 7 as i32],
    ]
];

for i in vec3d.iter() {

    // I must add another iter everytime I increment the dimension by 1.
    // Else, it returns the number of stored vector instead of the vector 
    // elements.

    for j in i.iter() { 
        n_vec_size += j.len() as i32;
    }
};

println!("3D vector elements :{}", n_vec_element); // prints 18

必须有一种更简洁的方法来执行此操作,但是我还没有弄清楚。最初,我尝试使用向量的len()函数,但是如上所述,它返回存储的向量数量而不是其元素。

1 个答案:

答案 0 :(得分:10)

您不需要显式循环:

let vec2d = vec![
    vec![1, 1, 1],
    vec![1, 1, 1],
];

let n_vec_element: usize = vec2d.iter().map(Vec::len).sum();

assert_eq!(n_vec_element, 6);

对于3d向量,您可以执行以下操作:

let vec3d = vec![
    vec![
        vec![1, 3, 5 as i32],
        vec![2, 4, 6 as i32],
        vec![3, 5, 7 as i32],
    ],
    vec![
        vec![1, 3, 5 as i32],
        vec![2, 4, 6 as i32],
        vec![3, 5, 7 as i32],
    ]
];

let n_vec_element: usize = vec3d.iter().flatten().map(Vec::len).sum();

assert_eq!(n_vec_element, 18);

使用4D向量,您可以放置​​2个flatten,等等。


借助专门化功能(即夜间编译器,即 ),您可以使用唯一的方法对此进行概括:

#![feature(specialization)]

trait Count {
    fn count(self) -> usize;
}

impl<T> Count for T {
    default fn count(self) -> usize {
        1
    }
}

impl<T> Count for T
where
    T: IntoIterator,
    T::Item: Count,
{
    fn count(self) -> usize {
        self.into_iter().map(|x| x.count()).sum()
    }
}

fn main() {
    let v = vec![1, 2, 3];
    assert_eq!(v.count(), 3);

    let v = vec![vec![1, 2, 3], vec![4, 5, 6]];
    assert_eq!(v.count(), 6);
}