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::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::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
queue::emplace() in C++ STL
Queue is also an abstract data type or a linear data structure, which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In a FIFO data structure, the first element added to the queue will be the first one to be removed. queue::emplace() This fu
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_strin
1 min read
STD::array in C++
The array is a collection of homogeneous objects and this array container is defined for constant size arrays or (static size). This container wraps around fixed-size arrays and the information of its size are not lost when declared to a pointer. In order to utilize arrays, we need to include the ar
5 min read
std::string::push_back() in C++
The std::string::push_back() method in C++ is used to append a single character at the end of string. It is the member function of std::string class defined inside <string> header file. In this article, we will learn about std::string::push_back() method in C++.Example:C++// C++ Program to ill
2 min read
std::string class in C++
C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. The string class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.String vs Character ArrayStringChar
8 min read
set operator= in C++ STL
The â=â is an operator in C++ STL which copies (or moves) a set to another set and set::operator= is the corresponding operator function. There are three versions of this function: The first version takes reference of an set as an argument and copies it to an set. Syntax: ums1.operator=(set &set
2 min read