是否可以按命令执行"cout"?
Is it possible to execute "cout" by order?
是否可以获得如图所示但具有特定顺序(从下到上)的原始输出?
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 循环,我可以移动线条并将它们的颜色一根一根地更改为白色再次延迟,但我不知道如何实际操作
#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;
}
如评论中所述,无法通过这种方式控制std::cout
。但是可以使用线程来为您控制时间。这是使用 <future>
中的 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;
}
是否可以获得如图所示但具有特定顺序(从下到上)的原始输出?
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 循环,我可以移动线条并将它们的颜色一根一根地更改为白色再次延迟,但我不知道如何实际操作
#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;
}
如评论中所述,无法通过这种方式控制std::cout
。但是可以使用线程来为您控制时间。这是使用 <future>
中的 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;
}