This C++ program illustrates use of output string stream. The class template std::basic_ostringstream implements output operations on memory based streams. This is used to attach stream to a string, that is we can write to it using this stream.
Here is the source code of the C++ program which illustrates use of output string stream. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to illustrate use of output string stream
*/
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string compose(int n, int id[], int age[], string name[])
{
ostringstream os;
os << "Id : " << id[n - 1] << endl
<< "Name : " << name[n - 1] << endl
<< "Age : " << age[n - 1] << endl;
return os.str();
}
int main()
{
int id[] = {1, 2, 3, 4, 5}, n;
int age[] = {21, 23, 22, 21, 20};
string name[] = {"Alice", "Bob", "Charles", "Danny", "Emily"};
cout << "Enter id (1 - 5) : ";
cin >> n;
cout << "Details\n";
cout << compose(n, id, age, name);
}
$ a.out Enter id (1 - 5) : 1 Details Id : 1 Name : Alice Age : 21
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Apply for Computer Science Internship
- Apply for C++ Internship
- Practice Computer Science MCQs
- Check Programming Books
- Check Computer Science Books