利用控制台输出编写的进度条程序,代码如下:
#pragma once
#include <iostream>
#include <mutex>
#include <thread>
class progressbar {
uint64_t total{0};
uint64_t cur{0};
FILE* f;
std::chrono::system_clock::time_point nextPoint = std::chrono::high_resolution_clock::now();
std::mutex m;
std::string unit;
public:
progressbar(std::string unit = "", FILE* f = stdout) : unit(std::move(unit)), f(f) {}
~progressbar() { fprintf(f, "[%lu%s/%lu%s]\n", cur, unit.c_str(), total, unit.c_str()); }
void set_len(size_t _total) { total = _total; }
void add(size_t n = 1) {
std::lock_guard<std::mutex> l(m);
cur += n;
if (cur >= total) {
fprintf(f, "[%lu%s/%lu%s]\r", total, unit.c_str(), total, unit.c_str());
fflush(f);
return;
}
auto curPoint = std::chrono::high_resolut