泛型算法系列14:random_shuffle()&&iter_swap()&&swap()

本文介绍了一个使用C++实现的随机数排列算法。通过自定义模板函数实现元素交换,并利用rand()函数产生随机数,对指定范围内的整数进行随机排序。最后展示了如何将一个包含0到19的整数序列打乱并输出。

fn crosvm_main<I: IntoIterator<Item = String>>(args: I) -> Result<CommandStatus> { let _library_watcher = sys::get_library_watcher(); // The following panic hook will stop our crashpad hook on windows. // Only initialize when the crash-pad feature is off. #[cfg(not(feature = "crash-report"))] sys::set_panic_hook(); // Ensure all processes detach from metrics on exit. #[cfg(windows)] let _metrics_destructor = metrics::get_destructor(); let args = prepare_argh_args(args); let args = args.iter().map(|s| s.as_str()).collect::<Vec<_>>(); let args = match crosvm::cmdline::CrosvmCmdlineArgs::from_args(&args[..1], &args[1..]) { Ok(args) => args, Err(e) if e.status.is_ok() => { // If parsing succeeded and the user requested --help, print the usage message to stdout // and exit with success. let help = shorten_usage(&e.output); println!("{help}"); return Ok(CommandStatus::SuccessOrVmStop); } Err(e) => { error!("arg parsing failed: {}", e.output); return Ok(CommandStatus::InvalidArgs); } }; let extended_status = args.extended_status; debug!("CLI arguments parsed."); let mut log_config = LogConfig { log_args: LogArgs { filter: args.log_level, proc_name: args.syslog_tag.unwrap_or("crosvm".to_string()), syslog: !args.no_syslog, ..Default::default() }, ..Default::default() }; let ret = match args.command { Command::CrossPlatform(command) => { // Past this point, usage of exit is in danger of leaking zombie processes. if let CrossPlatformCommands::Run(cmd) = command { if let Some(syslog_tag) = &cmd.syslog_tag { base::warn!( "`crosvm run --syslog-tag` is deprecated; please use \ `crosvm --syslog-tag=\"{}\" run` instead", syslog_tag ); log_config.log_args.proc_name.clone_from(syslog_tag); } // We handle run_vm separately because it does not simply signal success/error // but also indicates whether the guest requested reset or stop. run_vm(cmd, log_config) } else if let CrossPlatformCommands::Device(cmd) = command { // On windows, the device command handles its own logging setup, so we can't handle // it below otherwise logging will double init. if cfg!(unix) { syslog::init_with(log_config).context("failed to initialize syslog")?; } start_device(cmd) .map_err(|_| anyhow!("start_device subcommand failed")) .map(|_| CommandStatus::SuccessOrVmStop) } else { syslog::init_with(log_config).context("failed to initialize syslog")?; match command { #[cfg(feature = "balloon")] CrossPlatformCommands::Balloon(cmd) => { balloon_vms(cmd).map_err(|_| anyhow!("balloon subcommand failed")) } #[cfg(feature = "balloon")] CrossPlatformCommands::BalloonStats(cmd) => { balloon_stats(cmd).map_err(|_| anyhow!("balloon_stats subcommand failed")) } #[cfg(feature = "balloon")] CrossPlatformCommands::BalloonWs(cmd) => { balloon_ws(cmd).map_err(|_| anyhow!("balloon_ws subcommand failed")) } CrossPlatformCommands::Battery(cmd) => { modify_battery(cmd).map_err(|_| anyhow!("battery subcommand failed")) } #[cfg(feature = "composite-disk")] CrossPlatformCommands::CreateComposite(cmd) => create_composite(cmd) .map_err(|_| anyhow!("create_composite subcommand failed")), #[cfg(feature = "qcow")] CrossPlatformCommands::CreateQcow2(cmd) => { create_qcow2(cmd).map_err(|_| anyhow!("create_qcow2 subcommand failed")) } CrossPlatformCommands::Device(_) => unreachable!(), CrossPlatformCommands::Disk(cmd) => { disk_cmd(cmd).map_err(|_| anyhow!("disk subcommand failed")) } #[cfg(feature = "gpu")] CrossPlatformCommands::Gpu(cmd) => { modify_gpu(cmd).map_err(|_| anyhow!("gpu subcommand failed")) } #[cfg(feature = "audio")] CrossPlatformCommands::Snd(cmd) => { modify_snd(cmd).map_err(|_| anyhow!("snd command failed")) } CrossPlatformCommands::MakeRT(cmd) => { make_rt(cmd).map_err(|_| anyhow!("make_rt subcommand failed")) } CrossPlatformCommands::Resume(cmd) => { resume_vms(cmd).map_err(|_| anyhow!("resume subcommand failed")) } CrossPlatformCommands::Run(_) => unreachable!(), CrossPlatformCommands::Stop(cmd) => { stop_vms(cmd).map_err(|_| anyhow!("stop subcommand failed")) } CrossPlatformCommands::Suspend(cmd) => { suspend_vms(cmd).map_err(|_| anyhow!("suspend subcommand failed")) } CrossPlatformCommands::Swap(cmd) => { swap_vms(cmd).map_err(|_| anyhow!("swap subcommand failed")) } CrossPlatformCommands::Powerbtn(cmd) => { powerbtn_vms(cmd).map_err(|_| anyhow!("powerbtn subcommand failed")) } CrossPlatformCommands::Sleepbtn(cmd) => { sleepbtn_vms(cmd).map_err(|_| anyhow!("sleepbtn subcommand failed")) } CrossPlatformCommands::Gpe(cmd) => { inject_gpe(cmd).map_err(|_| anyhow!("gpe subcommand failed")) } CrossPlatformCommands::Usb(cmd) => { modify_usb(cmd).map_err(|_| anyhow!("usb subcommand failed")) } CrossPlatformCommands::Version(_) => { pkg_version().map_err(|_| anyhow!("version subcommand failed")) } CrossPlatformCommands::Vfio(cmd) => { modify_vfio(cmd).map_err(|_| anyhow!("vfio subcommand failed")) } #[cfg(feature = "pci-hotplug")] CrossPlatformCommands::VirtioNet(cmd) => { modify_virtio_net(cmd).map_err(|_| anyhow!("virtio subcommand failed")) } CrossPlatformCommands::Snapshot(cmd) => { snapshot_vm(cmd).map_err(|_| anyhow!("snapshot subcommand failed")) } } .map(|_| CommandStatus::SuccessOrVmStop) } } cmdline::Command::Sys(command) => { let log_args = log_config.log_args.clone(); // On windows, the sys commands handle their own logging setup, so we can't handle it // below otherwise logging will double init. if cfg!(unix) { syslog::init_with(log_config).context("failed to initialize syslog")?; } sys::run_command(command, log_args).map(|_| CommandStatus::SuccessOrVmStop) } }; sys::cleanup(); // WARNING: Any code added after this point is not guaranteed to run // since we may forcibly kill this process (and its children) above. ret.map(|s| { if extended_status { s } else { CommandStatus::SuccessOrVmStop } }) }
07-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的横打

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值