Is it possible to “prepare” input from cin?

后端 未结 2 468
情歌与酒
情歌与酒 2021-01-13 22:15

In his answer, specifically in the linked Ideone example, @Nawaz shows how you can change the buffer object of cout to write to something else. This made me thi

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-13 22:52

    #include 
    #include 
    
    int main()
    {
        std::stringstream s("32 7.4");
        std::cin.rdbuf(s.rdbuf());
    
        int i;
        double d;
        if (std::cin >> i >> d)
            std::cout << i << ' ' << d << '\n';
    }
    

提交回复
热议问题