小编Nil*_*nki的帖子

下标运算符中的逗号运算符?

我对逗号运算符很困惑。我从来没有见过这样的语法这样的代码?但我很好奇它是否在任何地方有用?为什么它在 c++20 中被弃用?

#include <iostream>
int main()
{
    int  a[5]{1,2,3,45,5};
    std::cout << a[(2,3)] <<'\n';  //  this is work , in c++17 works
    std::cout << a[2,3] << '\n';   // but this is deprecated in c++20 ,in c++17 works
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ c++17 c++20

5
推荐指数
1
解决办法
324
查看次数

获得错误的毫秒延迟值

我试图获得 1 毫秒的延迟,但我得到了 15 倍的延迟。我也尝试过使用 windowsSleep(1)函数,这也给了我相同的结果。

为什么我没有得到精确的毫秒延迟?

因为它有 1 秒的延迟。

#include <iostream>
#include <Windows.h>
#include <thread>
#include <chrono>

void counter1();

auto main() -> int
{

    std::thread p(&counter1);
    p.join();
    return 0;
}

void counter1()
{
    int nStep = 0;
    const int STEP = 1000;
    auto start = std::chrono::high_resolution_clock::now();
    for (;;)
    {
        ++nStep; // incrementing every millisecond
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
        if (nStep == STEP) {  // compares at second
            auto duration = std::chrono::high_resolution_clock::now() - start;
            std::cout << "counter took " <<
                std::chrono::duration_cast<std::chrono::seconds>(duration).count() …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading visual-studio c++-chrono

2
推荐指数
1
解决办法
80
查看次数

标签 统计

c++ ×2

c++-chrono ×1

c++17 ×1

c++20 ×1

multithreading ×1

visual-studio ×1