VOOZH about

URL: https://www.geeksforgeeks.org/cpp/std_quoted-in-cpp-14/

⇱ std::quoted in C++ 14 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

std::quoted in C++ 14

Last Updated : 11 Jul, 2024

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.

Syntax of std::quoted

The syntax for using std::quoted is as follows:

std::quoted(str, delim, esc);

Parameters

  • str: The string to be quoted or unquoted.
  • delim: The delimiter character to use for quoting. Default is the double-quote " character.
  • esc: The escape character to use for escaping special characters within the string. Default is the backslash \ character.

Return Value

The return value of the std::quoted depends on the operation it is being used with.

  • For output, std::quoted returns an std::ostream object that contains the quoted string.
  • For input, std::quoted returns an std::istream object from which the unquoted string can be read.

std::quoted for Output Operations

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.

Examples

Example 1: Simple program to demonstrate the use of std::quoted with output stream.

Input:
Hello, world!
cout << quoted(str)

Output:
"Hello, world!"

Output
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@
👁 std-quoted-for-output

Output
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,

std::quoted for Input Operations

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

Examples

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!
👁 std-quoted-for-input

Output
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!"

Output
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!"

Output
Welcome to @GeeksforGeeks
Comment
Article Tags:
Article Tags: