Defined in header <string> | ||
---|---|---|
string operator "" s(const char *str, std::size_t len); | (1) | (since C++14) |
u8string operator "" s(const char8_t *str, std::size_t len); | (2) | (since C++20) |
u16string operator "" s(const char16_t *str, std::size_t len); | (3) | (since C++14) |
u32string operator "" s(const char32_t *str, std::size_t len); | (4) | (since C++14) |
wstring operator "" s(const wchar_t *str, std::size_t len); | (5) | (since C++14) |
Forms a string literal of the desired type.
std::string{str, len}
std::u8string{str, len}
std::u16string{str, len}
std::u32string{str, len}
std::wstring{str, len}
str | - | pointer to the beginning of the raw character array literal |
len | - | length of the raw character array literal |
The string literal.
These operators are declared in the namespace std::literals::string_literals
, where both literals
and string_literals
are inline namespaces. Access to these operators can be gained with using namespace std::literals
, using namespace std::string_literals
, and using namespace std::literals::string_literals
.
std::chrono::duration
also defines operator""s
, to represent literal seconds, but it is an arithmetic literal: 10.0s
and 10s
are ten seconds, but "10"s
is a string.
#include <string> #include <iostream> int main() { using namespace std::string_literals; std::string s1 = "abc\0\0def"; std::string s2 = "abc\0\0def"s; std::cout << "s1: " << s1.size() << " \"" << s1 << "\"\n"; std::cout << "s2: " << s2.size() << " \"" << s2 << "\"\n"; }
Possible output:
s1: 3 "abc" s2: 8 "abc^@^@def"
constructs a basic_string (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/string/basic_string/operator_q__q_s