# ostream

***

**1. Basic Output**

```cpp
#include <iostream>

using namespace std;

int main() {
  cout << "Hello, world!" << endl;
  return 0;
}
```

**2. Formatted Output**

```cpp
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
  cout << fixed << setprecision(2);
  cout << "Pi is approximately " << 3.14159265 << endl;
  return 0;
}
```

**3. Output to a File**

```cpp
#include <iostream>
#include <fstream>

using namespace std;

int main() {
  ofstream outFile("output.txt");
  outFile << "Hello, file!" << endl;
  outFile.close();
  return 0;
}
```

**4. Output Redirection**

```cpp
#include <iostream>

using namespace std;

int main() {
  cout << "This output is redirected to stderr" << endl;
  cerr << "This output is redirected to stderr" << endl;
  return 0;
}
```

**5. Output Filtering**

```cpp
#include <iostream>
#include <ostream>
#include <iomanip>

using namespace std;

struct Filter : public ostream {
  Filter(ostream& out) : ostream(out) {
    rdbuf(out.rdbuf());
    this->setf(ios::fixed, ios::floatfield);
  }
};

int main() {
  Filter out(cout);
  out << setprecision(2) << "Pi is approximately " << 3.14159265 << endl;
  return 0;
}
```

**6. Output Custom Formatting**

```cpp
#include <iostream>
#include <ostream>
#include <iomanip>

using namespace std;

struct CustomFloatFormat : public ostream {
  CustomFloatFormat(ostream& out) : ostream(out) {
    rdbuf(out.rdbuf());
    this->setf(ios::fixed, ios::floatfield);
    this->setf(ios::right, ios::adjustfield);
    this->setf(ios::showpos);
  }
};

int main() {
  CustomFloatFormat out(cout);
  out << setprecision(2) << setw(10) << "Pi is approximately " << 3.14159265 << endl;
  return 0;
}
```

**7. Output Manipulators**

```cpp
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
  cout << left << setw(10) << "Name" << right << setw(10) << "Score" << endl;
  cout << left << setw(10) << "John" << right << setw(10) << 95 << endl;
  return 0;
}
```

**8. Output to a String**

```cpp
#include <iostream>
#include <sstream>

using namespace std;

int main() {
  stringstream ss;
  ss << "Hello, world!" << endl;
  cout << ss.str();
  return 0;
}
```

**9. Output to a Buffer**

```cpp
#include <iostream>
#include <sstream>

using namespace std;

int main() {
  stringstream ss;
  ss << "Hello, world!" << endl;
  cout.write(ss.str().c_str(), ss.str().length());
  return 0;
}
```

**10. Output to a Stream Buffer**

```cpp
#include <iostream>
#include <sstream>

using namespace std;

int main() {
  stringstream ss;
  ss << "Hello, world!" << endl;
  cout.rdbuf(ss.rdbuf());
  cout << "This output goes to the string stream." << endl;
  return 0;
}
```

**11. Output to a File and Console**

```cpp
#include <iostream>
#include <fstream>

using namespace std;

int main() {
  ofstream outFile("output.txt");
  streambuf *coutbuf = cout.rdbuf();
  cout.rdbuf(outFile.rdbuf());
  cout << "This output goes to the file and console." << endl;
  cout.rdbuf(coutbuf);
  return 0;
}
```

**12. Output to a Vector**

```cpp
#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<string> outputVector;
  string line;
  while (getline(cin, line)) {
    outputVector.push_back(line);
  }
  for (auto& line : outputVector) {
    cout << line << endl;
  }
  return 0;
}
```

**13. Output to a List**

```cpp
#include <iostream>
#include <list>

using namespace std;

int main() {
  list<string> outputList;
  string line;
  while (getline(cin, line)) {
    outputList.push_back(line);
  }
  for (auto& line : outputList) {
    cout << line << endl;
  }
  return 0;
}
```

**14. Output to a Deque**

```cpp
#include <iostream>
#include <deque>

using namespace std;

int main() {
  deque<string> outputDeque;
  string line;
  while (getline(cin, line)) {
    outputDeque.push_back(line);
  }
  for (auto& line : outputDeque) {
    cout << line << endl;
  }
  return 0;
}
```

**15. Output to a Set**

```cpp
#include <iostream>
#include <set>

using namespace std;

int main() {
  set<string> outputSet;
  string line;
  while (getline(cin, line)) {
    outputSet.insert(line);
  }
  for (auto& line : outputSet) {
    cout << line << endl;
  }
  return 0;
}
```

**16. Output to a Map**

```cpp
#include <iostream>
#include <map>

using namespace std;

int main() {
  map<string, string> outputMap;
  string key, value;
  while (getline(cin, key) && getline(cin, value)) {
    outputMap.insert(make_pair(key, value));
  }
  for (auto& p : outputMap) {
    cout << p.first << ": " << p.second << endl;
  }
  return 0;
}
```

**17. Output to a Tuple**

```cpp
#include <iostream>
#include <tuple>

using namespace std;

int main() {
  tuple<int, string, double> outputTuple(1, "John", 3.14159265);
  cout << get<0>(outputTuple) << " " << get<1>(outputTuple) << " " << get<2>(outputTuple) << endl;
  return 0;
}
```

**18. Output to a Pair**

```cpp
#include <iostream>
#include <utility>

using namespace std;

int main() {
  pair<int, string> outputPair(1, "John");
  cout << outputPair.first << " " << outputPair.second << endl;
  return 0;
}
```

**19. Output to a File with Append**

```cpp
#include <iostream>
#include <fstream>

using namespace std;

int main() {
  ofstream outFile("output.txt", ios::app);
  outFile << "This output is appended to the file." << endl;
  outFile.close();
  return 0;
}
```

**20. Output to a File with Truncate**

```cpp
#include <iostream>
#include <fstream>

using namespace std;

int main() {
  ofstream outFile("output.txt", ios::trunc);
  outFile << "This output truncates the file." << endl;
  outFile.close();
  return 0;
}
```

**21. Output to a File with Binary Mode**

```cpp
#include <iostream>
#include <fstream>

using namespace std;

int main() {
  

```
