[c++] std::quoted
When used with the input stream (std::istream
), std::quoted
manipulator reads a string enclosed in quotes from the input stream, allowing you to extract quoted fields from the input.
Here’s an example of how std::quoted
can be used:
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string input = "John,Doe,\"123 Main St\",New York";
std::istringstream iss(input);
std::string name, surname, address, city;
std::getline(iss, name, ',');
std::getline(iss, surname, ',');
iss >> std::quoted(address);
std::getline(iss >> std::ws, city); // Using std::ws to consume leading white space
std::cout << "Name: " << name << ", Surname: " << surname << ", Address: " << address << ", City: " << city << std::endl;
return 0;
}
In this example, std::quoted
is used to extract the quoted address field from the input string, “123 Main St”.
By using std::quoted
, you can gracefully handle the quoted fields in a delimited string.
For more information, you can refer to the cppreference page.