Open In App

std::quoted in C++ 14

Last Updated : 11 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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!"
C++
// C++ Program to illustrate std::quote in C++14
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    // Define a string variable with formatted content
    string input = "Hello, Geeks!";

    // Display the original content
    cout << "Original content: " << input << endl;

    // Output the quoted content using quoted
    cout << "Quoted content: " << quoted(input) << endl;

    return 0;
}

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 C++
// C++ Program to illustrate std::quote with custorm
// delimiter and escape character
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    // Define a string variable with formatted content
    string input = "Hello, @world@!\nWelc@me";

    // Display the original content
    cout << "Original content: " << input << endl;

    // Output the quoted content using quoted
    cout << "Quoted content: " << quoted(input, '@', '\n')
         << endl;

    return 0;
}

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 C++
// C++ Program to illustrate std::quote with input operation
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    // creating input string stream
    istringstream input("\"Hello, \\\world!\"");
    string str;
    // using default delimiter and escape character
    input >> quoted(str);
    cout << str << endl;
    return 0;
}

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!"
C++
// C++ Program to illustrate std::quoted for string without
// the starting quote
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    // creating input string stream
    istringstream input("Hello, \\\world!\"");
    string str;
    // using default delimiter and escape character
    input >> quoted(str);
    cout << str << endl;
    return 0;
}

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!"
C++
// C++ Program to illustrate std::quoted for custom custom
// delimiter and escape character
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    // creating input string stream
    istringstream input("@Welcome _to _@GeeksforGeeks@");
    string str;
    // using default delimiter and escape character
    input >> quoted(str, '@', '_');
    cout << str << endl;
    return 0;
}

Output
Welcome to @GeeksforGeeks



Next Article
Article Tags :
Practice Tags :

Similar Reads