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>

source

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))
source

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.