gpt4 book ai didi

c++ - 当更多信息打印到屏幕上时,使 boost::progress_display 工作

转载 作者:太空狗 更新时间:2023-10-29 20:20:58 25 4
gpt4 key购买 nike

我有一个控制台程序,它的计算可能需要一些时间。我正在使用 boost::progress_display 向用户提供一些反馈。

我的问题是,如果发生某些事情,我还想将其他更新打印到标准输出,这会破坏进度条:

0%   10   20   30   40   50   60   70   80   90   100%
|----|----|----|----|----|----|----|----|----|----|
**Found temporary candidate with score: 40
Found temporary candidate with score: 46
*Found temporary candidate with score: 52
********Found temporary candidate with score: 55
**Found temporary candidate with score: 67
**************************************

是否有一种简单的方法来同时拥有进度条(理想情况下在代码中与 boost::progress_display 一样无干扰)和屏幕更新?

编辑:在评论中提出我没有提供我正在寻找的示例的建议后,我想要类似于以下内容的代码:

boost::progress_display progress(10);
for (size_t i = 0; i< 10; ++i)
{
std::cout << "Number is: " << i << "\n";
++progress;
}

但这不会导致此输出:

0%   10   20   30   40   50   60   70   80   90   100%
|----|----|----|----|----|----|----|----|----|----|
Number is: 0
*****Number is: 1
*****Number is: 2
*****Number is: 3
*****Number is: 4
*****Number is: 5
*****Number is: 6
*****Number is: 7
*****Number is: 8
*****Number is: 9
******

相反,我希望在进度条之后或之前显示 Number is: z 行:

Number is: 0
Number is: 1
Number is: 2
Number is: 3
Number is: 4
Number is: 5
Number is: 6
Number is: 7
Number is: 8
Number is: 9
0% 10 20 30 40 50 60 70 80 90 100%
|----|----|----|----|----|----|----|----|----|----|
***************************************************

最佳答案

使用自制类(class)

我无法使用 boost::progress_display 找到一个简单的答案,所以我实现了我自己的 RAII 类,它在 Linux 下工作得很好:

进度条.h

#ifndef PROGRESS_BAR_H
#define PROGRESS_BAR_H

#include <string>

/**
* RAII implementation of a progress bar.
*/
class ProgressBar
{
public:
/**
* Constructor.
* It takes two values: the expected number of iterations whose progress we
* want to monitor and an initial message to be displayed on top of the bar
* (which can be updated with updateLastPrintedMessage()).
*/
ProgressBar(
uint32_t expectedIterations, const std::string& initialMessage="");

/**
* Destructor to guarantee RAII.
*/
~ProgressBar();

// Make the object non-copyable
ProgressBar(const ProgressBar& o) = delete;
ProgressBar& operator=(const ProgressBar& o) = delete;

/**
* Must be invoked when the progress bar is no longer needed to restore the
* position of the cursor to the end of the output.
* It is automatically invoked when the object is destroyed.
*/
void endProgressBar();

/**
* Prints a new message under the last printed message, without overwriting
* it. This moves the progress bar down to be placed under the newly
* written message.
*/
void printNewMessage(const std::string& message);

/**
* Prints a message while the progress bar is on the screen on top on the
* last printed message. Since the cursor is right at the beginning of the
* progress bar, it moves the cursor up by one line before printing, and
* then returns it to its original position.
*/
void updateLastPrintedMessage(const std::string& message);

/**
* Overloaded prefix operator, used to indicate that the has been a new
* iteration.
*/
void operator++();

private:
unsigned int mTotalIterations;
unsigned int mNumberOfTicks;
bool mEnded;
size_t mLengthOfLastPrintedMessage;
};

#endif /* PROGRESS_BAR_H */

进度条.cpp

#include "ProgressBar.h"
#include <iostream>
#include <iomanip>
#include <sstream>

#define LENGTH_OF_PROGRESS_BAR 55
#define PERCENTAGE_BIN_SIZE (100.0/LENGTH_OF_PROGRESS_BAR)

namespace
{
std::string generateProgressBar(unsigned int percentage)
{
const int progress = static_cast<int>(percentage/PERCENTAGE_BIN_SIZE);
std::ostringstream ss;
ss << " " << std::setw(3) << std::right << percentage << "% ";
std::string bar("[" + std::string(LENGTH_OF_PROGRESS_BAR-2, ' ') + "]");

unsigned int numberOfSymbols = std::min(
std::max(0, progress - 1),
LENGTH_OF_PROGRESS_BAR - 2);

bar.replace(1, numberOfSymbols, std::string(numberOfSymbols, '|'));

ss << bar;
return ss.str();
}
}

ProgressBar::ProgressBar(
uint32_t expectedIterations, const std::string& initialMessage)
: mTotalIterations(expectedIterations),
mNumberOfTicks(0),
mEnded(false)
{
std::cout << initialMessage << "\n";
mLengthOfLastPrintedMessage = initialMessage.size();
std::cout << generateProgressBar(0) << "\r" << std::flush;
}

ProgressBar::~ProgressBar()
{
endProgressBar();
}

void ProgressBar::operator++()
{
if (mEnded)
{
throw std::runtime_error(
"Attempted to use progress bar after having terminated it");
}

mNumberOfTicks = std::min(mTotalIterations, mNumberOfTicks+1);
const unsigned int percentage = static_cast<unsigned int>(
mNumberOfTicks*100.0/mTotalIterations);

std::cout << generateProgressBar(percentage) << "\r" << std::flush;
}

void ProgressBar::printNewMessage(const std::string& message)
{
if (mEnded)
{
throw std::runtime_error(
"Attempted to use progress bar after having terminated it");
}

std::cout << "\r"
<< std::left
<< std::setw(LENGTH_OF_PROGRESS_BAR + 6)
<< message << "\n";
mLengthOfLastPrintedMessage = message.size();
const unsigned int percentage = static_cast<unsigned int>(
mNumberOfTicks*100.0/mTotalIterations);

std::cout << generateProgressBar(percentage) << "\r" << std::flush;

}

void ProgressBar::updateLastPrintedMessage(const std::string& message)
{
if (mEnded)
{
throw std::runtime_error(
"Attempted to use progress bar after having terminated it");
}

std::cout << "\r\033[F"
<< std::left
<< std::setw(mLengthOfLastPrintedMessage)
<< message << "\n";
mLengthOfLastPrintedMessage = message.size();
}

void ProgressBar::endProgressBar()
{
if (!mEnded)
{
std::cout << std::string(2, '\n');
}
mEnded = true;
}

如何使用

这是两段代码,展示了如何使用它以及预期的输出:

更新.cpp

#include "ProgressBar.h"
#include <unistd.h>

#define ITERATIONS 10

int main()
{
ProgressBar progress(ITERATIONS, "No odd number found");
for (size_t i = 0; i < ITERATIONS; ++i)
{
// Sleep for 0.5s so that the gif is clear enough
usleep(500000);

if (i%2 != 0)
{
progress.updateLastPrintedMessage(
"New odd number found: " + std::to_string(i));
}

++progress;
}
}

updating.cpp 的输出

enter image description here

appending.cpp

#include "ProgressBar.h"
#include <unistd.h>

#define ITERATIONS 10

int main()
{
ProgressBar progress(ITERATIONS, "Looking for odd numbers...");
for (size_t i = 0; i < ITERATIONS; ++i)
{
// Sleep for 0.5s so that the gif is clear enough
usleep(500000);

if (i%2 != 0)
{
progress.printNewMessage(
"New odd number found: " + std::to_string(i));
}

++progress;
}
}

appending.cpp 的输出

enter image description here

关于c++ - 当更多信息打印到屏幕上时,使 boost::progress_display 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46717885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com