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;
}
OutputOriginal 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@
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;
}
OutputOriginal 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!
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;
}
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;
}
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;
}
OutputWelcome to @GeeksforGeeks
Similar Reads
std::plus in c++
It is a Function object for performing addition. The object class whose call returns the result of adding its two arguments (as returned by operator + ). Syntax : template struct plus : binary_function { T operator() (const T& x, const T& y) const { return x + y; } }; Template parameters : T
2 min read
std::multiplies in C++
Function object for performing multiplication. Effectively calls operator* on two instances of type T. Syntax : template struct multiplies : binary_function { T operator() (const T& x, const T& y) const {return x*y;} }; Template Parameters : T - Type of the arguments and return type of the f
2 min read
std::stof in C++
Parses string interpreting its content as a floating-point number, which is returned as a value of type float. Syntax : float stof (const string& str, size_t* idx = 0); float stof (const wstring& str, size_t* idx = 0); Parameters : str : String object with the representation of a floating-po
2 min read
std::make_unique in C++ 14
std::make_unique is a utility function in C++ that was introduced in C++14. It is used to create a unique_ptr object, which is a smart pointer that manages the lifetime of dynamically allocated objects. It is defined inside <memory> header file. Syntaxstd::make_unique <object_type> (argu
2 min read
std::minus in C++
Binary function object class whose call returns the result of subtracting its second argument from its first argument (as returned by the binary operator -). Syntax : template struct minus : binary_function { T operator() (const T& x, const T& y) const {return x-y;} }; Template parameters :
2 min read
std::search in C++
std::search is defined in the header file <algorithm> and used to find out the presence of a subsequence satisfying a condition (equality if no such predicate is defined) with respect to another sequence. It searches the sequence [first1, last1) for the first occurrence of the subsequence defi
4 min read
std::memchr in C++
C++ offers various standard template library functions to be used. One of them is memchr() function which is used to search for the first occurrence of a character in a specified number of characters. memchr() is defined inside <cstring> header file. Syntax of memchr()const void* memchr( const
2 min read
std::count() in C++ STL
In C++, the count() is a built-in function used to find the number of occurrences of an element in the given range. This range can be any STL container or an array. In this article, we will learn about the count() function in C++. Letâs take a quick look at a simple example that uses count() method:
3 min read
std::source_location in C++ 20
The latest C++ standard version 20, brought about a fresh header file called <source_location> Header. It has an efficient method of obtaining details of a function or expression's source location while running. This functionality is immensely beneficial for debugging and profiling activities
3 min read
std::to_string in C++
In C++, the std::to_string function is used to convert numerical values into the string. It is defined inside <string> header and provides a simple and convenient way to convert numbers of any type to strings. In this article, we will learn how to use std::to_string() in C++. Syntaxstd::to_str
1 min read