小编mis*_*coo的帖子

为什么即使流有一些char,in_avail()输出为零?

#include <iostream>
int main( )
{
   using namespace std;
   cout << cin.rdbuf()->in_avail() << endl;
   cin.putback(1);
   cin.putback(1);
   cout << cin.rdbuf()->in_avail() << endl;
   return 0;
} //compile by g++-4.8.1
Run Code Online (Sandbox Code Playgroud)

我想这会输出0和2

但是当我运行代码时,它输出0和0,为什么?

或者如果我改变cin.putback(1); 到一个; cin >> a; 输入12 12;

它仍然输出0和0

c++

8
推荐指数
2
解决办法
2003
查看次数

为什么std :: timed_mutex :: try_lock_for不起作用?

我使用gcc-4.8.1(configure:./ configure --prefix =/usr/local)在Ubuntu 12.04中编译以下代码,但是当我运行它时,它不起作用.它没有停下来等待互斥锁.它返回了假,并且输出了"Hello world!"

命令:g ++ -std = c ++ 11 main.cpp -omain -pthread

当我使用gcc-4.6(apt-get install g ++)编译它时,它运行良好.该节目等了大约十秒钟,并且超过了"Hello world!"

#include <thread>
#include <iostream>
#include <chrono>
#include <mutex>

std::timed_mutex test_mutex;

void f()
{
    test_mutex.try_lock_for(std::chrono::seconds(10));
    std::cout << "hello world\n";
}

int main()
{
    std::lock_guard<std::timed_mutex> l(test_mutex);
    std::thread t(f);
    t.join();
      return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11

6
推荐指数
1
解决办法
1595
查看次数

func()+ func()是否未定义行为?

我只知道i = i++;未定义的行为,但如果在表达式中调用了两个或更多函数,并且所有函数都相同.这是不确定的?例如:

int func(int a)
{
    std::cout << a << std::endl;
    return 0;
}

int main()
{
    std::cout << func(0) + func(1) << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ undefined-behavior unspecified-behavior

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