#include <iostream>
#include <iomanip>
#include <chrono>
#include <thread>
void showProgressBar(double progress) {
const int barWidth = 70;
std::cout << "\r[";
int pos = static_cast<int>(barWidth * progress);
for (int i = 0; i < barWidth; ++i) {
if (i < pos) std::cout << "=";
else if (i == pos) std::cout << ">";
else std::cout << " ";
}
std::cout << "] " << std::setw(3) << static_cast<int>(progress * 100.0) << "% ";
std::cout << std::flush;
}
int main() {
// 模拟下载进度
for (double progress = 0.0; progress <= 1.0; progress += 0.01) {
showProgressBar(progress);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
std::cout << std::endl;
return 0;
}
效果如下,只作为参考,可自行开发
标签:std,cout,int,barWidth,c++,进度,linux,progress,include From: https://www.cnblogs.com/itsfei/p/18194232