[英]What is the most efficient way of taking a number of integer user inputs and storing it in a Vec<i32>?
I was trying to use rust for competitive coding and I was wondering what is the most efficient way of storing user input in a Vec.我试图使用 rust 进行竞争性编码,我想知道在 Vec 中存储用户输入的最有效方法是什么。 I have come up with a method but I am afraid that it is slow and redundant.
我想出了一个方法,但我担心它很慢而且多余。
Here is my code:这是我的代码:
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input).expect("cant read line");
let input:Vec<&str> = input.split(" ").collect();
let input:Vec<String> = input.iter().map(|x| x.to_string()).collect();
let input:Vec<i32> = input.iter().map(|x| x.trim().parse().unwrap()).collect();
println!("{:?}", input);
}
PS: I am new to rust. PS:我是生锈的新手。
I see those ways of improving performance of the code:我看到了提高代码性能的那些方法:
std::io::stdin()
, std::io::BufReader
may have great effect for reading eg from std::fs::File
.std::io::stdin()
并不真正相关,但std::io::BufReader
可能对从std::fs::File
读取例如有很大的影响。 Buffer capacity can also matter.let si = std::io::stdin(); let si = si.locked();
let si = std::io::stdin(); let si = si.locked();
let si = std::io::stdin(); let si = si.locked();
extend_from_iter
instead of collect
, if the code reads multiple line (unlike in the sample you posted in the question).extend_from_iter
而不是collect
来避免分配。Iterator
operations together.Iterator
操作链接在一起。 Or using a loop like for line in input.split(...) { ... }
.for line in input.split(...) { ... }
的循环。 It may affect performance in both ways - you need to experiment to find out.to_string()
and just storing reference to input buffer (which can also be used to parse()
into i32
. Note that this may invite famous Rust borrowing and lifetimes complexity.to_string()
并仅存储对输入缓冲区的引用(也可用于parse()
到i32
中。请注意,这可能会导致著名的 Rust 借用和生命周期复杂性。parse()
.parse()
。Vec
first.Vec
。 This can be beneficial especially if multiple threads can be used.i32
.i32
。Yeah, there are some changes you can make that will make your code more precise, simple and faster.是的,您可以进行一些更改,以使您的代码更加精确、简单和快速。
A better code :更好的代码:
use std::io;
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let input: Vec<i32> = input.split_whitespace().map(|x| x.parse().unwrap()).collect();
println!("{:?}", input);
}
The input.split_whitespace()
returns an iterator containing elements that are seperated by any kind of whitespace including line breaks. input.split_whitespace()
返回一个迭代器,其中包含由任何类型的空格(包括换行符)分隔的元素。 This saves the time used in spliting by just one whitespace input.split(" ")
and iterating over again with a .trim()
method on each string slice to remove any surronding whitespaces.这节省了仅用一个空格
input.split(" ")
进行拆分的时间,并在每个字符串切片上使用.trim()
方法再次迭代以删除任何周围的空格。 (You can also checkout input.split_ascii_whitespace()
, if you want to restrict the split over ascii whitespaces). (如果您想限制对 ascii 空格的拆分,您也可以签
input.split_ascii_whitespace()
)。
There was no need for the code input.iter().map(|x| x.to_string()).collect()
, since you can call also call a .trim()
method on a string slice.不需要代码
input.iter().map(|x| x.to_string()).collect()
,因为您也可以在字符串切片上调用.trim()
方法。
This saves some time in both the runtime and coding process, since the .collect()
method is only used once and there was just one iteration.这在运行时和编码过程中都节省了一些时间,因为
.collect()
方法只使用一次并且只有一次迭代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.