Home C++ Iostream Functions tie() C++ std::ios::tie() Function Definition In C++, tie() connects a stream to an output stream. Before every input or output on the first stream, the tied output stream is flushed first.
That is why a prompt written with cout appears before cin waits for you: std::cin is tied to std::cout by default. Include <iostream> to use it.
This is a member function of std::ios. You call it with a dot on a stream object, for example std::cin, std::cout, a file stream, or a std::stringstream. A stream is simply C++'s way to read or write data one piece after another.
Syntax Call tie with no argument to get the tied stream. Pass a pointer to set a new one.
std::ostream* t = std::cin.tie(); // get
std::cin.tie(nullptr); // untie
std::cin.tie(&std::cout); // tie again Call What it does std::cin.tie()Returns a pointer to the tied stream, or nullptr. std::cin.tie(nullptr)Removes the tie. cin no longer flushes cout. in.tie(&out)Ties in to out and returns the old tied stream.
Untie only in programs that care about speed, and make sure the prompts are still flushed some other way.
Library declarations ostream* tie() const;
ostream* tie(ostream* tiestr); Parameters Parameter Description tiestrOptional. A pointer to the output stream to tie, or nullptr for no tie. Without it, tie() only returns the current value.
Return Value Return Value Description std::ostream*A pointer to the stream that was tied before the call, or nullptr if there was none.
C++ Examples 1. Default tie 2. Untie 3. Custom tie Example 1 — cin Is Tied to cout by Default Ask cin.tie() which stream it is linked to.
#include <iostream>
int main() {
std::ostream* t = std::cin.tie();
std::cout << "cin is tied to cout: " << (t == &std::cout ? "yes" : "no") << '\n';
std::cout << "cout is tied to something: " << (std::cout.tie() ? "yes" : "no") << '\n';
return 0;
} cin is tied to cout: yes
cout is tied to something: no How It Works std::cin.tie() returns a pointer. The program compares it with the address of std::cout.They match, so cin is tied to cout: every read from the keyboard flushes cout first. cout itself is not tied to anything, so its tie() is nullptr, which counts as false.Example 2 — Untie cin for Speed cin.tie(nullptr) removes the link and returns the old stream, so you can put it back.
#include <iostream>
int main() {
std::ostream* old = std::cin.tie(nullptr);
std::cout << "tied now: " << (std::cin.tie() ? "yes" : "no") << '\n';
std::cin.tie(old);
std::cout << "tied again: " << (std::cin.tie() ? "yes" : "no") << '\n';
return 0;
} tied now: no
tied again: yes How It Works tie(nullptr) removes the link. It returns the stream that was tied, and the program stores it in old.Now std::cin.tie() is nullptr, so the first line prints no. std::cin.tie(old) restores the link, so the second line prints yes.Example 3 — Tie Two Streams of Your Own Any input stream can be tied to any output stream. Here both are string streams.
#include <iostream>
#include <sstream>
int main() {
std::ostringstream log;
std::istringstream in("5");
std::ostream* before = in.tie(&log);
std::cout << "was tied before: " << (before ? "yes" : "no") << '\n';
std::cout << "tied to log: " << (in.tie() == &log ? "yes" : "no") << '\n';
return 0;
} was tied before: no
tied to log: yes How It Works A new string stream has no tie, so in.tie(&log) returns nullptr. The first line prints no because that returned pointer is empty. in.tie() now returns the address of log, so log will be flushed before each input on in.Frequently Asked Questions What does cin.tie() do in C++? It returns the output stream that is tied to cin. By default that is cout, so cout is flushed before each read from cin.
What does cin.tie(nullptr) do? It removes the tie, so reading from cin no longer flushes cout. Together with sync_with_stdio(false) it makes input faster.
Why does my prompt not appear after cin.tie(nullptr)? Without the tie, cout is not flushed before cin waits. Write endl or << std::flush after the prompt.
What is returned by tie(ptr)? The pointer that was tied before the call, so you can restore it later.
Can I tie any stream to any other? Yes, an input or output stream can be tied to one output stream. A stream can be tied to only one at a time.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
14 people found this page helpful
Helpful Share Copy link Suggestion