To insert a NULL character.
template< class charT, class traits > basic_ostream<charT, traits> &ends (basic_ostream<charT,traits>& os);
The manipulator ends, takes no external arguments, but is placed in the stream. It inserts a NULL character into the stream, usually to terminate a string.
A reference to ostream. The this pointer is returned.
The ostringstream provides in-core character streams but must be null terminated by the user. The manipulator ends provides a null terminator.
#include <iostream> #include <sstream> int main() { using namespace std; ostringstream out; // see note above out << "Ask the teacher anything\n"; out << "OK, what is 2 + 2?\n"; out << 2 << " plus " << 2 << " equals " << 4 << ends; cout << out.str(); return 0; }
Result:
Ask the teacher anything OK, what is 2 + 2? 2 plus 2 equals 4?