Muhammad Umer
Muhammad Umer

Reputation: 18097

How to redirect input stream to output stream in one line?

I want to do this:

cout<< cin;

Instead of this:

int x;
cin>>x;
cout<<x;

I have tried this:

cout<< (cin>>); //no luck

I hope it's clear that what i want.

Upvotes: 5

Views: 2306

Answers (3)

Columbo
Columbo

Reputation: 60989

Or a sexy method, for a change:

std::cout << std::cin.rdbuf();

Upvotes: 7

iwolf
iwolf

Reputation: 1062

One byte at a time: std::cout.put(std::cin.get());

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726629

Once you turn off skipping of whitespace, you should be able to do it in one string, but it's going to be a pretty long string:

std::copy(
    std::istream_iterator<char>(std::cin)
,   std::istream_iterator<char>()
,   std::ostream_iterator<char>(std::cout,"")
);

Demo.

Upvotes: 3

Related Questions