std::ws(3)
π NAME
std::ws β okay so it's literally std::ws, bestie, that's the whole name π«Ά (and yes, the vibe is "eat the whitespace, leave the good part")
From the C++ Standard Library, no cap. β¨
β¨ SYNOPSIS
It's defined in the header <istream>, and here's the signature, exactly as written, don't come for me:
template< class CharT, class Traits >
std::basic_istream<CharT, Traits>& ws( std::basic_istream<CharT, Traits>& is );
So what does it do? It discards leading whitespace from an input stream. That's the whole personality. π
It behaves as an UnformattedInputFunction, except that is.gcount() is not modified. (Yes, that exception matters. Remember it. π
)
After constructing and checking the sentry object, it extracts characters from the stream and discards them until any one of these things happens:
- an end of file condition occurs in the input sequence β in which case the function calls
setstate(eofbit)but does not setfailbit. β οΈ This does not apply ifeofbitis already set onisprior to the call tows, because in that case the construction of the sentry object would setfailbit. (Messy, I know. That's the rule though, fr fr.) - the next available character
cin the input sequence is not whitespace, as determined bystd::isspace(c, is.getloc()). That non-whitespace character is not extracted β it stays right where it is, main character energy. π
It's an input-only I/O manipulator, so you can call it in an expression like in >> std::ws for any in of type std::basic_istream. π¦
πΈ PARAMETERS
isβ reference to the input stream. That's it. That's the parameter. π
π RETURN VALUE
Returns is β a reference to the stream, after it ate all those consecutive whitespace characters. Slay. β¨
π§ NOTES
If eofbit is set on the stream prior to the call, the construction of the sentry object will set failbit. So likeβ¦ heads up. π
π₯ EXAMPLE
// Run this code
#include <iomanip>
#include <iostream>
#include <istream>
#include <sstream>
#include <string>
int main()
{
for (const char* str : {" #1 test", "\t #2 test", "#3 test"})
{
std::string line;
std::getline(std::istringstream{str}, line);
std::cout << "getline returns:\t" << std::quoted(line) << '\n';
std::istringstream iss{str};
std::getline(iss >> std::ws, line);
std::cout << "ws + getline returns:\t" << std::quoted(line) << '\n';
}
}
β OUTPUT
getline returns: " #1 test"
ws + getline returns: "#1 test"
getline returns: " #2 test"
ws + getline returns: "#2 test"
getline returns: "#3 test"
ws + getline returns: "#3 test"
See how the whitespace justβ¦ vanished? Iconic. π β¨
π DEFECT REPORTS
These are the behavior-changing defect reports that got applied retroactively to previously published C++ standards β yes, time travel, we love to see it. π°οΈ
LWG 415β applied to C++98.- Behavior as published: calling
std::wsmight not construct the sentry object (inconsistent with other input functions). - Correct behavior: it's required to construct the sentry object. π
- Behavior as published: calling
π¦ SEE ALSO
ignoreβ extracts and discards characters until the given character is found (public member function ofstd::basic_istream<CharT,Traits>). Different vibe, same "throw it away" energy. ποΈ
okay that's the whole page, you ate that up honestly ππ β this lil writeup came from cppreference.com, dated 2024.06.10, and the manual page is std::ws(3). go forth and strip that whitespace, bestie πΈβ¨π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms