What Is std::quoted Quoted String In Modern C++?
Sometimes we want to preserve the string format especially when we use string in a string with /”. In C++14 and above, there is a std::quoted template that allows handling strings safely where they may contain spaces and special characters and it keeps their formatting intact. In this post, we explain std::quoted quoted strings in modern C++. What Is Quoted String In Modern C++? The std::quoted template is included in header, and it is used to handle strings (i.e. “Let’s learn from ”LearnCPlusPlus.org!” “) safely where they may contain spaces and special characters and it keeps their formatting intact. Here is the syntax, std::quoted( ) Here are C++14 templates defined in where the string is used as input, template quoted( const CharT* s, CharT delim = CharT(‘”‘), CharT escape = CharT(‘\’) ); or template quoted( const std::basic_string& s, CharT delim = CharT(‘”‘), CharT escape = CharT(‘\’) ); Here is a C++14 template defined in where the string is used as output, template quoted( std::basic_string& s, CharT delim=CharT(‘”‘), CharT escape=CharT(‘\’) ); Note that this feature is is using std::basic_string and it improved in C++17 by the std::basic_string_view support. What Is std::quoted quoted string in modern C++? Here is an example that uses input string and outputs into a stringstream: const std::string str = “I say “LearnCPlusPlus!””; std::stringstream sstr; sstr std::quoted(str_out); // output sstr to str_out Is there a full example about std::quoted quoted string in modern C++? Here is a full example about std::quoted in modern C++. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include #include #include int main() { std::stringstream sstr; const std::string str = “Let’s learn from “LearnCPlusPlus.org!” “; sstr
