【问题标题】:Is it possible to execute "cout" by order?是否可以按顺序执行“cout”?
【发布时间】:2020-11-24 20:41:37
【问题描述】:

是否可以获得如图所示的原始输出,但具有特定的顺序(从下到上)?

cout<<R"(programming )";sleep(10); cout<<R"(
          newbie at  )";sleep(5); cout<<R"(
            i'm      )"; sleep(1);


output:  programming------>printed third at the first line
           newbie at------->printed second at the second line
            i'm--------->printed first at the third line

注意:我的意思不是反转输出。

编辑:我想简单的技巧是将它们的颜色设置为黑色,这将使它们“消失”,然后通过使用嵌套的 for 循环,我可以通过线条移动并将它们的颜色一一更改为白色延迟,但我不知道如何实际操作

【问题讨论】:

  • 带有计时器的线程可以完成这项工作。
  • 感谢您的评论.....有什么好书或资源来学习 CPP 中的线程使用吗?
  • 据我所知,没有。 cout 不控制输出行位置。
  • 颠倒顺序和以相反顺序获得输出有什么区别?
  • 好吧....控制输出行的位置很方便

标签: c++ c++11 c++14 c++17


【解决方案1】:
#include <chrono>
#include <future>
#include <iostream>
#include <thread>

int main()
{
    using namespace std::chrono_literals;

    auto print_first_line = std::async(std::launch::async, []() {
        std::this_thread::sleep_for(6s);
        std::cout << "programming\n" << std::flush;
    });
    auto print_second_line = std::async(std::launch::async, []() {
        std::this_thread::sleep_for(3s);
        std::cout << "newbie at\n" << std::flush;
    });
    auto print_third_line = std::async(std::launch::async, []() {
        std::this_thread::sleep_for(1s);
        std::cout << "i'm\n" << std::flush;
    });

    print_first_line.wait();
    print_second_line.wait();
    print_third_line.wait();

    return 0;
}

如 cmets 中所述,无法以这种方式控制 std::cout。但是可以使用线程来为您控制时间。这是一个使用&lt;future&gt; 中的std::async 对打印语句进行多线程处理的快速而肮脏的示例。

std::async 的第一个参数明确告诉它异步启动,因为它可以在不创建新线程的情况下启动。第二个参数是一个带有睡眠定时器和打印语句的 lambda。

值得注意的是,您可能需要一个额外的编译器参数。我当前的测试是在 WSL (Debian) 中进行的,所以我使用的编译命令是 clang++ -Wall -Wextra -pthread main.cpp 其中-pthread 是启用多线程的参数。对于您的平台,可能会有所不同。

正如建议的那样,这种情况可能会更好地使用更通用的功能;类似delay_print()。这是一个可能的实现。

#include <array>
#include <chrono>
#include <cstdint>
#include <future>
#include <iostream>
#include <string>
#include <thread>

void delay_print(const std::chrono::duration<std::int64_t> &time,
                 std::string message)
{
    std::this_thread::sleep_for(time);
    std::cout << message << std::flush;
}

int main()
{
    using namespace std::chrono_literals;

    std::array<int, 4> seconds{7, 4, 2, 1};
    std::array<std::string, 4> phrases{"How\n", "Now\n", "Brown\n", "Cow\n"};

    auto print_line_01 =
        std::async(std::launch::async, &delay_print,
                   std::chrono::seconds(seconds[0]), phrases[0]);
    auto print_line_02 =
        std::async(std::launch::async, &delay_print,
                   std::chrono::seconds(seconds[1]), phrases[1]);
    auto print_line_03 =
        std::async(std::launch::async, &delay_print,
                   std::chrono::seconds(seconds[2]), phrases[2]);
    auto print_line_04 =
        std::async(std::launch::async, &delay_print,
                   std::chrono::seconds(seconds[3]), phrases[3]);

    print_line_01.wait();
    print_line_02.wait();
    print_line_03.wait();
    print_line_04.wait();

    return 0;
}

【讨论】:

  • 你甚至可以创建函数/lambda delay_print
  • 那将是一个更好、更通用的解决方案。
  • 做得很好,非常感谢,但现在我编辑了我的问题,你能快速检查一下是否可行吗?
  • 如果只是说间距,只需要加上间距即可。编辑并没有改变任何东西。我正在实施建议的delay_print()
  • 如果您希望以相同的顺序打印文本,但希望它向上书写,您将需要类似 ncurses 的东西。终端不能像开箱即用那样工作。您实际上将创建一个 CLI“GUI”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多