In C++, ws is an input manipulator. When you write in >> std::ws, the stream reads and throws away all whitespace characters in front: spaces, tabs, and new lines. It stops at the first character that is not whitespace.
The most useful place for it is before getline(), to skip the leftover new line after cin >> number. 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::ws into an input stream with >>, or pass the stream to getline() that way.
C++
std::cin >> n >> std::ws; // skip whitespace after n
std::getline(std::cin >> std::ws, line); // skip, then read a line
Call
What it does
in >> std::ws
Skips all whitespace in front of the next character.
std::getline(in >> std::ws, line)
Skips whitespace first, then reads a line.
in >> n >> std::ws
Reads n, then removes the whitespace that follows.
ws skips all whitespace, including empty lines. ignore() skips only to the end of one line.
Library declarations
C++
template<class CharT, class Traits>
basic_istream<CharT, Traits>& ws(basic_istream<CharT, Traits>& is);
Parameters
Parameter
Description
is
The input stream. It is passed in for you when you write in >> ws. You never write it yourself.
Return Value
Return Value
Description
istream&
A reference to the same stream. If the end of the input is reached, eofbit is set.
C++ Examples
Example 1 — Skip Spaces Before getline()
The leading spaces are removed, so the line starts with the first real letter.