c++ - 如何在控制台c ++中制作加载栏
问题描述
如何制作一个以加载栏形式显示进度的功能?看起来像这样的东西[----------->]
解决方案
下面的代码,改编自这个问题。请注意,我添加Windows.h
了使用该Sleep
功能。这只是为了展示它是如何工作的。您可以简单地删除它,或将其更改为 *nix 工作替代方案。该函数异步运行,因此您可以在进度条尚未完全加载时执行其他操作。
#include <iostream>
#include <future>
#include <thread>
#include <Windows.h>
void load() {
float progress = 0.0;
while (progress < 1.0) {
int barWidth = 70;
int pos = barWidth * progress;
Sleep(100);
std::cout << "[";
for (int i = 0; i < barWidth; i++) {
if (i < pos) std::cout << "=";
else if (i == pos) std::cout << ">";
else std::cout << " ";
}
std::cout << "]" << int(progress * 100.0) << " %\r";
std::cout.flush();
progress += 0.01;
}
std::cout << std::endl;
}
int main() {
std::future<void> startLoading = std::async(std::launch::async, load);
// Do something while loading...
for (int i = 0; i < 100; i++) {
std::cout << i << " ";
}
return 0;
}
推荐阅读
- python-3.x - 在 al 列中查找最常见的字符串
- mysql - Node.js 与 knex + Mysql 迁移错误,而重命名列导致默认值
- c# - Asp.net Core 如何将 ReflectionIT 与 ViewModel 一起使用?
- angularjs - 从 AngularJs 转换为 Angular 2+
- database - 我应该使用 InfluxDB 或 Elasticsearch 或其他方法来存储网站的鼠标跟踪数据吗?
- javascript - Javascript regexp - 删除引号之间的空格
- angular - VSCode 终端中的“Ctrl + 单击以跟随链接”不会跳转到源文件但会打开新的浏览器选项卡
- php - 多维数组,同一个key的值求和
- ansible - 是否有一个 Ansible 模块可以帮助在启动后保存 iptables 配置
- php - 如何获取从未完成的数据