std::literals::string_literals::operator""s
STD::文字::String[医]文字::运算符“s”
Defined in header | | |
---|---|---|
string operator "" s(const char *str, std::size_t len | (1) | (since C++14) |
u16string operator "" s(const char16_t *str, std::size_t len | (2) | (since C++14) |
u32string operator "" s(const char32_t *str, std::size_t len | (3) | (since C++14) |
wstring operator "" s(const wchar_t *str, std::size_t len | (4) | (since C++14) |
形成所需类型的字符串文本。
1%29std::string
{str, len}
2%29std::u16string
{str, len}
3%29std::u32string
{str, len}
4%29std::wstring
{str, len}
参数
str | - | pointer to the beginning of the raw character array literal |
---|---|---|
len | - | length of the raw character array literal |
返回值
字符串文字。
注记
这些运算符在命名空间中声明。std::literals::string_literals
,两者都是literals
和string_literals
是内联命名空间。可以通过以下方式访问这些操作员using namespace std::literals
,,,using namespace std::string_literals
,和using namespace std::literals::string_literals
...
std::chrono::duration
还定义operator""s
,以表示文字秒,但它是算术文字:10.0s
和10s
是十秒钟,但是"10"s
是一根绳子。
例
二次
#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";
}
二次
可能的产出:
二次
s1: 3 "abc"
s2: 8 "abc^@^@def"
二次
另见
(constructor) | constructs a basic_string (public member function) |
---|
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
http://en.cppreference.com/w/cpp/string/basic[医]字符串/运算符[医]q[医][医]q[医]斯