Struct template_cp::iom::Scanner
source · pub struct Scanner<R> { /* private fields */ }
Expand description
Scanner is a struct that can be used to parse input from a reader.
Examples
use cp_template::Scanner;
use std::io;
fn main() {
let mut scan = Scanner::new(io::stdin().lock());
let mut out = io::BufWriter::new(io::stdout().lock());
solve(&mut scan, &mut out);
}
fn solve<R: io::BufRead, W: io::Write>(scan: &mut Scanner<R>, out: &mut W) {
let n: usize = scan.tok();
let v: Vec<i32> = (0..n).map(|_| scan.tok()).collect();
writeln!(out, "{} {v:?}", n).ok();
}
use cp_template::Scanner;
use std::io;
fn main() {
let mut scan = read_file("in.txt").unwrap();
let mut out = writer_file("out.txt").unwrap();
solve(&mut scan, &mut out);
}
fn solve<R: io::BufRead, W: io::Write>(scan: &mut Scanner<R>, out: &mut W) {
let n: usize = scan.tok();
let v: Vec<i32> = (0..n).map(|_| scan.tok()).collect();
writeln!(out, "{} {v:?}", n).ok();
}
Implementations§
source§impl<R: BufRead> Scanner<R>
impl<R: BufRead> Scanner<R>
sourcepub fn new(reader: R) -> Self
pub fn new(reader: R) -> Self
Creates a new Scanner.
Examples
use cp_template::Scanner;
let mut scan = Scanner::new(std::io::stdin().lock());
use cp_template::Scanner;
let file = std::fs::File::open(filename)?;
Scanner::new(io::BufReader::new(file))
sourcepub fn tok<T: FromStr>(&mut self) -> T
pub fn tok<T: FromStr>(&mut self) -> T
Reads a token.
Panics
Panics if the token can’t be parsed or the input is empty.
Examples
use cp_template::Scanner;
let mut scan = Scanner::new(std::io::stdin().lock());
let n: i32 = scan.tok();
use cp_template::Scanner;
let mut scan = Scanner::new(std::io::stdin().lock());
let n = scan.tok::<i32>();
Auto Trait Implementations§
impl<R> RefUnwindSafe for Scanner<R>where R: RefUnwindSafe,
impl<R> Send for Scanner<R>where R: Send,
impl<R> Sync for Scanner<R>where R: Sync,
impl<R> Unpin for Scanner<R>where R: Unpin,
impl<R> UnwindSafe for Scanner<R>where R: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more