template
class basic_string_view;
basic_string_view提供对字符串的只读访问,它并不占有对字符串的所有权,提供对字符串轻量和安全的访问。从实现上来说,basic_string_view只包含指向字符串的指针以及字符串的大小。标准库中定义的字符串视图类型有:
std::string_view std::basic_string_view
std::wstring_view std::basic_string_view
std::u8string_view std::basic_string_view
std::u16string_view std::basic_string_view
std::u32string_view std::basic_string_view
本文章的代码库为:
https://gitee.com/gamestorm577/CppStd
可以用basic_string_view、字符串以及迭代器来构造一个basic_string_view。默认构造函数构造一个空的字符串视图。由于basic_string类型可以隐式地转换为basic_string_view类型,所有也可以用一个basic_string来构造basic_string_view。代码示例:
char tmp1[] = "abcde";
std::string tmp2 = "12345";
std::string_view str_view1;
std::string_view str_view2(tmp1);
std::string_view str_view3(tmp1, 3);
std::string_view str_view4(str_view3);
std::string_view str_view5(tmp2.begin(), tmp2.begin() + 3);
std::string_view str_view6(tmp2);
std::cout << "str_view1 = " << str_view1 << std::endl;
std::cout << "str_view2 = " << str_view2 << std::endl;
std::cout << "str_view3 = " << str_view3 << std::endl;
std::cout << "str_view4 = " << str_view4 << std::endl;
std::cout << "str_view5 = " << str_view5 << std::endl;
std::cout << "str_view6 = " << str_view6 << std::endl;
输出结果为:
str_view1 =
str_view2 = abcde
str_view3 = abc
str_view4 = abc
str_view5 = 123
str_view6 = 12345
可以用basic_string_view或者basic_string来赋值basic_string_view。代码示例:
std::string tmp = "12345";
std::string_view view1 = tmp;
std::string_view view2 = view1;
std::cout << "view1 = " << view1 << std::endl;
std::cout << "view2 = " << view2 << std::endl;
输出结果:
view1 = 12345
view2 = 12345
接口begin、cbegin指向basic_string_view起始的迭代器,end、cend指向末尾的迭代器。rbegin、crbegin指向起始的逆向迭代器,rend、crend指向末尾的逆向迭代器。代码示例为:
std::string_view view("abcdef", 3);
for (auto iter = view.begin(); iter != view.end(); ++iter)
{
std::cout << *iter << std::endl;
}
输出结果为:
a
b
c
返回指定位置的字符。示例代码:
std::string_view view("1234567");
char c = view[2];
std::cout << "c = " << c << std::endl;
输出结果:
c = 3
返回指定位置的字符。示例代码:
std::string_view view("1234567");
char c = view.at(2);
std::cout << "c = " << c << std::endl;
输出结果:
c = 3
返回首个字符。示例代码:
std::string_view view("1234567");
char c = view.front();
std::cout << "c = " << c << std::endl;
输出结果:
c = 1
返回最后一个字符。示例代码:
std::string_view view("1234567");
char c = view.back();
std::cout << "c = " << c << std::endl;
输出结果:
c = 7
返回字符串的首地址。代码示例:
std::string_view view("1234567");
const char* data = view.data();
std::cout << data << std::endl;
输出结果:
1234567
返回字符的数量。示例代码:
std::string_view view("1234567");
std::string_view::size_type size = view.size();
std::string_view::size_type length = view.length();
std::cout << "size = " << size << std::endl;
std::cout << "length = " << length << std::endl;
输出结果:
size = 7
length = 7
返回系统允许的最大字符个数。代码示例:
std::string_view view;
std::string_view::size_type max = view.max_size();
std::cout << "max size = " << max << std::endl;
可能的输出结果:
max size = 18446744073709551615
检查字符串是否为空。代码示例:
std::string_view view1;
std::string_view view2("");
std::string_view view3("1234567");
std::cout << std::boolalpha;
std::cout << "view1 empty: " << view1.empty() << std::endl;
std::cout << "view2 empty: " << view2.empty() << std::endl;
std::cout << "view3 empty: " << view3.empty() << std::endl;
输出结果:
view1 empty: true
view2 empty: true
view3 empty: false
特殊值,通常用来表示不存在的位置。代码示例:
std::string_view::size_type npos = std::string_view::npos;
std::cout << "npos = " << npos << std::endl;
可能的输出结果为:
npos = 18446744073709551615
将basic_string_view保存的首字符向前移动n个位置:
void remove_prefix(size_type n);
代码示例:
std::string_view view = "1234567";
std::cout << "view = " << view << std::endl;
view.remove_prefix(3);
std::cout << "view = " << view << std::endl;
输出结果为:
view = 1234567
view = 4567
将basic_string_view保存的末尾字符向前移动n个位置:
void remove_suffix(size_type n);
代码示例:
std::string_view view = "1234567";
std::cout << "view = " << view << std::endl;
view.remove_suffix(3);
std::cout << "view = " << view << std::endl;
输出结果:
view = 1234567
view = 1234
交换两个字符串视图的内容。代码示例:
std::string_view view1 = "1234567";
std::string_view view2 = "abc";
view1.swap(view2);
std::cout << "view1 = " << view1 << std::endl;
std::cout << "view2 = " << view2 << std::endl;
输出结果:
view1 = abc
view2 = 1234567
把字符串视图中的指定字符拷贝到目标字符串中:
size_type copy(_CharT* dst, size_type count, size_type pos);
示例代码:
char str[20] = {0};
std::string_view view = "123456789";
view.copy(str, 5, 3);
std::cout << "str = " << str << std::endl;
输出结果:
str = 45678
返回字符串视图的一个子视图:
basic_string_view substr(size_type pos, size_type count);
代码示例:
std::string_view view = "123456789";
std::string_view sub_view = view.substr(4, 5);
std::cout << "sub_view = " << sub_view << std::endl;
输出结果:
sub_view = 56789
比较两个视图。代码示例:
std::string_view view1 = "12345678";
std::string_view view2 = "234";
int ret = 0;
ret = view1.compare(view2);
std::cout << "case1, ret = " << ret << std::endl;
ret = view1.compare(1, 3, view2);
std::cout << "case2, ret = " << ret << std::endl;
ret = view1.compare(1, 3, view2, 0, 2);
std::cout << "case3, ret = " << ret << std::endl;
ret = view1.compare("234");
std::cout << "case4, ret = " << ret << std::endl;
ret = view1.compare(1, 3, "234");
std::cout << "case5, ret = " << ret << std::endl;
ret = view1.compare(1, 3, "234", 0, 2);
std::cout << "case6, ret = " << ret << std::endl;
输出结果:
case1, ret = -1
case2, ret = 0
case3, ret = 1
case4, ret = -1
case5, ret = 0
case6, ret = 1
判断是否以某个字符或者字符串开头。代码示例:
std::string_view view1 = "12345678";
std::string_view view2 = "123";
bool ret = false;
std::cout << std::boolalpha;
ret = view1.starts_with(view2);
std::cout << "case1, ret = " << ret << std::endl;
ret = view1.starts_with("23");
std::cout << "case2, ret = " << ret << std::endl;
ret = view1.starts_with('1');
std::cout << "case3, ret = " << ret << std::endl;
输出结果:
case1, ret = true
case2, ret = false
case3, ret = true
判断是否以某个字符或者字符串结尾。代码示例:
std::string_view view1 = "12345678";
std::string_view view2 = "678";
bool ret = false;
std::cout << std::boolalpha;
ret = view1.ends_with(view2);
std::cout << "case1, ret = " << ret << std::endl;
ret = view1.ends_with("67");
std::cout << "case2, ret = " << ret << std::endl;
ret = view1.ends_with('8');
std::cout << "case3, ret = " << ret << std::endl;
输出结果:
case1, ret = true
case2, ret = false
case3, ret = true
找到给定位置之后的第一个和给定字符串相同的位置。代码示例:
std::string_view view1 = "123456781234567812345678";
std::string_view view2 = "234";
std::string_view::size_type pos = 0;
pos = view1.find(view2, 5);
std::cout << "find pos = " << pos << std::endl;
pos = view1.find('2', 5);
std::cout << "find pos = " << pos << std::endl;
pos = view1.find("23456789", 5, 3);
std::cout << "find pos = " << pos << std::endl;
pos = view1.find("234", 5);
std::cout << "find pos = " << pos << std::endl;
输出结果:
find pos = 9
find pos = 9
find pos = 9
find pos = 9
找到给定位置之前的最后一个和给定字符串相同的位置。代码示例:
std::string_view view1 = "123456781234567812345678";
std::string_view view2 = "234";
std::string_view::size_type pos = 0;
pos = view1.rfind(view2, 15);
std::cout << "find pos = " << pos << std::endl;
pos = view1.rfind('2', 15);
std::cout << "find pos = " << pos << std::endl;
pos = view1.rfind("23456789", 15, 3);
std::cout << "find pos = " << pos << std::endl;
pos = view1.rfind("234", 15);
std::cout << "find pos = " << pos << std::endl;
输出结果:
find pos = 9
find pos = 9
find pos = 9
find pos = 9
从给定位置后找到首个等于给定字符串中的某个字符的位置。代码示例:
std::string_view view1 = "123123451231234512312345123";
std::string_view view2 = "45";
std::string_view::size_type pos = 0;
pos = view1.find_first_of(view2, 8);
std::cout << "find pos = " << pos << std::endl;
pos = view1.find_first_of('4', 8);
std::cout << "find pos = " << pos << std::endl;
pos = view1.find_first_of("4512", 8, 2);
std::cout << "find pos = " << pos << std::endl;
pos = view1.find_first_of("45", 8);
std::cout << "find pos = " << pos << std::endl;
输出结果:
find pos = 14
find pos = 14
find pos = 14
find pos = 14
从给定位置前找到最后一个等于给定字符串中的某个字符的位置。代码示例:
std::string_view view1 = "123123451231234512312345123";
std::string_view view2 = "45";
std::string_view::size_type pos = 0;
pos = view1.find_last_of(view2, 21);
std::cout << "find pos = " << pos << std::endl;
pos = view1.find_last_of('5', 21);
std::cout << "find pos = " << pos << std::endl;
pos = view1.find_last_of("4512", 21, 2);
std::cout << "find pos = " << pos << std::endl;
pos = view1.find_last_of("45", 21);
std::cout << "find pos = " << pos << std::endl;
输出结果:
find pos = 15
find pos = 15
find pos = 15
find pos = 15
从给定位置后找到首个不等于给定字符串中的任何一个字符的位置。代码示例:
std::string_view view1 = "123456789765123456789765123456789765";
std::string_view view2 = "1234567";
std::string_view::size_type pos = 0;
pos = view1.find_first_not_of(view2, 10);
std::cout << "find pos = " << pos << std::endl;
pos = view1.find_first_not_of('1', 10);
std::cout << "find pos = " << pos << std::endl;
pos = view1.find_first_not_of("12345678", 10, 7);
std::cout << "find pos = " << pos << std::endl;
pos = view1.find_first_not_of("1234567", 10);
std::cout << "find pos = " << pos << std::endl;
输出结果:
find pos = 19
find pos = 10
find pos = 19
find pos = 19
从给定位置前找到最后一个不等于给定字符串中的任何一个字符的位置。代码示例:
std::string_view view1 = "123456789765123456789765123456789765";
std::string_view view2 = "1234567";
std::string_view::size_type pos = 0;
pos = view1.find_last_not_of(view2, 29);
std::cout << "find pos = " << pos << std::endl;
pos = view1.find_last_not_of('1', 29);
std::cout << "find pos = " << pos << std::endl;
pos = view1.find_last_not_of("123456789", 29, 7);
std::cout << "find pos = " << pos << std::endl;
pos = view1.find_last_not_of("1234567", 29);
std::cout << "find pos = " << pos << std::endl;
输出结果:
find pos = 20
find pos = 29
find pos = 20
find pos = 20
operator==、!=、<、<=、>、>=用于两个basic_string_view的比较。代码示例:
std::string_view view1 = "12345";
std::string_view view2 = "123";
std::cout << std::boolalpha;
std::cout << "view1 == view2: " << (view1 == view2) << std::endl;
std::cout << "view1 != view2: " << (view1 != view2) << std::endl;
std::cout << "view1 > view2 : " << (view1 > view2) << std::endl;
std::cout << "view1 >= view2: " << (view1 >= view2) << std::endl;
std::cout << "view1 < view2 : " << (view1 < view2) << std::endl;
std::cout << "view1 <= view2: " << (view1 <= view2) << std::endl;
输出结果:
view1 == view2: false
view1 != view2: true
view1 > view2 : true
view1 >= view2: true
view1 < view2 : false
view1 <= view2: false
用于字符串视图的流输出。代码示例:
std::string_view view = "12345";
std::cout << "view = " << view << std::endl;
输出结果:
view = 12345
从字符数组字面量组成basic_string_view:
using namespace std::literals;
std::string_view view1 = "abc\0\0def";
std::string_view view2 = "abc\0\0def"sv;
std::cout << "view1 size = " << view1.size() << ", view1 = " << view1
<< std::endl;
std::cout << "view2 size = " << view2.size() << ", view2 = " << view2
<< std::endl;
输出结果:
view1 size = 3, view1 = abc
view2 size = 8, view2 = abcdef
计算字符串视图的哈希值。示例代码:
std::hash hash;
std::string_view view1 = "1234567";
std::string_view view2 = "1234";
std::cout << "view1 hash = " << hash(view1) << std::endl;
std::cout << "view2 hash = " << hash(view2) << std::endl;
输出结果:
view1 hash = 16051747686835230389
view2 hash = 16197558426068326720