In C++, ends writes one null character, the character with code 0, to an output stream. A null character marks the end of a C string.
Do not mix it up with endl. endl ends a line. ends ends a string. It is rarely needed today. Include <iostream> to use it.
This is a manipulator. You do not call it on its own. You send it into a stream with << or >>, and it changes how that stream behaves from then on (or does one small job, like ending a line).
Syntax
Put std::ends into an output stream.
C++
std::ostringstream out;
out << "ab" << std::ends; // the text is a, b, and a zero character
Call
What it does
out << std::ends
Writes one character with the code 0.
out << "ab" << std::ends
Writes three characters: a, b, and the null character.
out << std::endl
Different: writes a new line and flushes.
With std::string and std::ostringstream you do not need ends. str() already gives a safe string.
Library declarations
C++
template<class CharT, class Traits>
basic_ostream<CharT, Traits>& ends(basic_ostream<CharT, Traits>& os);
Parameters
Parameter
Description
os
The output stream. It is passed in for you when you write out << ends. You never write it yourself.
Return Value
Return Value
Description
ostream&
A reference to the same stream, so you can keep chaining <<.
C++ Examples
Example 1 — See That ends Adds a Character
Compare the length of the text with and without ends.