![]() |
VOOZH | about |
std::quoted is an I/O manipulator function that was introduced in C++ 14 as the part of <iomanip> library. Its primary purpose is to handle the quoted string in the input and output operations. In this article, we will learn about the std::quoted manipulator, how it works and how to use it in our C++ program.
The syntax for using std::quoted is as follows:
std::quoted(str, delim, esc);
The return value of the std::quoted depends on the operation it is being used with.
std::quoted returns an std::ostream object that contains the quoted string.std::quoted returns an std::istream object from which the unquoted string can be read.For output operation using << (insertion operator) on the given stream, the std::quoted will add the given delimiter at the and the end of the given string. Along with that, it will add the given escape character to the place where there is a delimiter or escape character already present in the string.
Example 1: Simple program to demonstrate the use of std::quoted with output stream.
Input:
Hello, world!
cout << quoted(str)
Output:
"Hello, world!"
Original content: Hello, Geeks! Quoted content: "Hello, Geeks!"
As we can see, the std::quoted() function wraps the string in the default delimiter (").
Example 2: Program to demonstrate the use of std::quoted with custom delimiter and escape character.
We can also use the std::quoted to tokenize and quote the given string using the custom delimiter and custom escape sequence to std::quoted.
Input:
Hello, @world@!\nWelc@me
cout << quoted(str, '@', '\n')
Output:
@Hello,
@world
@!
Welc
@me@
Original content: Hello, @world@! Quoted content: @Hello, @world @!@
The '@' replaced the default delimiter and the place where the '@' or '\n' is already present in the string, it added the given escape sequence character '\n' effectively printing the tokenized version quoted with the given delimiter.
But we have to know that the string as a whole will be quoted not the individual words that it tokenize. For Example, if for the string: "first,second,third", you want to get the output: 'first','second','third', it will not be possible using quoted because for the given delimiter (,) and escape character ('), it will output: ,first',second',third,
For input operation using >> (extraction operator) on the given stream, std::quoted does the opposite of what it does for output operation.
The std::quote unquote the quoted string provided in the input and remove every escape sequence from the input text. Let's understand the working of the std::quoted for input operations
Example 1: Simple program to demonstrate the use of std::quoted with output stream.
Input:
"Hello, \\\world!"
Output:
cin >> std::quoted(str)
str = Hello, world!
Hello, world!
Note: If the input string does not contain the starting quote, the std::quoted just reads the string till the first whitespace without doing any changes.
Example 2: Program to demonstrate the behaviour of std::quote with unquoted string input.
Input:
Hello, \\\world!"
Output:
cin >> std::quoted(str)
str = Hello, \\\world!"
Hello,
Example 3: Program to demonstrate the behaviour of std::quote with input string with custom delimiter and escape character
Input:
@Welcome _to _@GeeksforGeeks@
Output:
cin >> std::quoted(str, '@', '_')
str = Hello, \\\world!"
Welcome to @GeeksforGeeks