字符串库(1)-std::basic_string

std::basic_string是为操作任何字符类型的字符串设计的模板类:

template
class basic_string;

namespace pmr 
{
template >
using basic_string = std::basic_string<_CharT, _Traits, polymorphic_allocator<_CharT>>;
}

标准库中定义的字符串类型有:

std::string	           std::basic_string
std::wstring	       std::basic_string
std::u8string          std::basic_string
std::u16string         std::basic_string
std::u32string         std::basic_string
std::pmr::string       std::pmr::basic_string
std::pmr::wstring      std::pmr::basic_string
std::pmr::u8string     std::pmr::basic_string
std::pmr::u16string    std::pmr::basic_string
std::pmr::u32string    std::pmr::basic_string

本位的代码库为:

https://gitee.com/gamestorm577/CppStd

构造和赋值

构造函数

可以用字符串、字符、basic_string、迭代器、字符列表以及sting_view构造basic_string对象,示例代码为:

std::vector s_arr = {'a', 'b', 'c', 'd', 'e'};

std::string str1;
std::string str2("Hello_world!");
std::string str3(10, 'a');
std::string str4(str2, 5);
std::string str5(str2, 5, 2);
std::string str6("Hello_world!", 7);
std::string str7(&s_arr[1], &s_arr[4]);
std::string str8{'a', 'c', 'b'};
std::string_view view = str2;
std::string str9(view);

std::cout << "str1: " << str1 << std::endl;
std::cout << "str2: " << str2 << std::endl;
std::cout << "str3: " << str3 << std::endl;
std::cout << "str4: " << str4 << std::endl;
std::cout << "str5: " << str5 << std::endl;
std::cout << "str6: " << str6 << std::endl;
std::cout << "str7: " << str7 << std::endl;
std::cout << "str8: " << str8 << std::endl;
std::cout << "str9: " << str9 << std::endl;

输出结果为:

str1: 
str2: Hello_world!
str3: aaaaaaaaaa
str4: _world!
str5: _w
str6: Hello_w
str7: bcd
str8: acb
str9: Hello_world!

赋值函数

用于替换字符串的内容,可以用另一个basic_string、字符串、字符、字符列表以及string_view赋值,示例代码为:

std::string tmp = "Hello_world";
std::string str;

str = tmp;
std::cout << "str = " << str << std::endl;

str = "another string";
std::cout << "str = " << str << std::endl;

str = 'a';
std::cout << "str = " << str << std::endl;

str = {'a', 'b', 'd', 'c'};
std::cout << "str = " << str << std::endl;

std::string_view view = tmp;
str = view;
std::cout << "str = " << str << std::endl;

输出结果为:

str = Hello_world
str = another string
str = a
str = abdc
str = Hello_world

当参数为basic_string时,有拷贝赋值和移动赋值两种方式。如果赋值对象的字符串内容是保存在堆上的,并且赋值的方式是移动赋值,那么赋值操作完成后,原对象将不再占有字符串内容。代码示例为:

std::string tmp(50, 'a');
std::string str;
std::cout << "Before assign, tmp: " << tmp << std::endl;
std::cout << "Before assign, str: " << str << std::endl;
str = std::move(tmp);
std::cout << "After  assign, tmp: " << tmp << std::endl;
std::cout << "After  assign, str: " << str << std::endl;

输出结果为:

Before assign, tmp: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Before assign, str: 
After  assign, tmp: 
After  assign, str: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

assign

替换原basic_string中的字符串,可以传入的参数有字符、basic_string、字符串、迭代器、字符列表以及string_view。代码示例为:

std::string tmp = "hello_world!";
std::string view = tmp;
std::vector s_arr = {'e', 'd', 'c', 'b', 'a'};
std::string str;

str.assign(10, 'a');
std::cout << "str: " << str << std::endl;

str.assign(tmp);
std::cout << "str: " << str << std::endl;

str.assign(tmp, 4, 4);
std::cout << "str: " << str << std::endl;

str.assign("abcdef", 3);
std::cout << "str: " << str << std::endl;

str.assign("defc");
std::cout << "str: " << str << std::endl;

str.assign(&s_arr[1], &s_arr[4]);
std::cout << "str: " << str << std::endl;

str.assign({'a', 'a', 'b'});
std::cout << "str: " << str << std::endl;

str.assign(view);
std::cout << "str: " << str << std::endl;

str.assign(view, 4, 3);
std::cout << "str: " << str << std::endl;

输出结果为:

str: aaaaaaaaaa
str: hello_world!
str: o_wo
str: abc
str: defc
str: dcb
str: aab
str: hello_world!
str: o_w

当参数为basic_string的右值引用时,如果参数对象的字符串内容是保存在堆上的,并且赋值的方式是移动赋值,那么赋值操作完成后,原对象将不再占有字符串内容。代码示例为:

std::string tmp(50, 'a');
std::string str;
std::cout << "Before assign, tmp: " << tmp << std::endl;
std::cout << "Before assign, str: " << str << std::endl;
str.assign(std::move(tmp));
std::cout << "After  assign, tmp: " << tmp << std::endl;
std::cout << "After  assign, str: " << str << std::endl;

输出结果为:

Before assign, tmp: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Before assign, str: 
After  assign, tmp: 
After  assign, str: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

元素访问

at

返回指定位置的字符的引用,代码示例为:

std::string str = "abcdef";
char& ch = str.at(3);
std::cout << "ch = " << ch << std::endl;
ch = d

当元素的位置超过范围时,会抛出std::out_of_range,代码示例为:

std::string str = "abcdef";
try
{
    char& ch = str.at(100);
}
catch (std::out_of_range exc)
{
    std::cout << "out of range: " << exc.what() << std::endl;
}

输出结果为:

out of range: basic_string

operator[]

返回指定位置的字符的引用。如果位置参数超过了范围,该行为未定义。代码示例为:

std::string str = "abcdef";
char& ch = str[3];
std::cout << "ch = " << ch << std::endl;

输出结果为:

ch = d

front

返回首字符的引用,要求字符串不为空,代码示例为:

std::string str = "abcdef";
char& ch = str.front();
std::cout << "ch = " << ch << std::endl;

输出结果为:

ch = a

back

返回最后一个字符的引用,要求字符串不为空,代码示例为:

std::string str = "abcdef";
char& ch = str.back();
std::cout << "ch = " << ch << std::endl;

输出结果为:

ch = f

data

返回保存的字符串首字符的指针,代码示例为:

std::string str = "abc";
char* data = str.data();
std::cout << data << std::endl;

输出结果为:

abc

c_str

返回保存的字符串首字符的const类型的指针,代码示例为:

std::string str = "abc";
const char* c_str = str.c_str();
std::cout << c_str << std::endl;

输出结果为:

abc

operator basic_string_view

用于函数参数中将basic_string类型隐式转换为basic_string_view类型,代码示例为:

auto func = [](std::string_view str) -> void
{
    std::cout << "str = " << str << std::endl;
};

std::string str = "hello world";
func(str);

输出结果为:

str = hello world

迭代器

接口begin、cbegin指向basic_string起始的迭代器,end、cend指向末尾的迭代器。rbegin、crbegin指向起始的逆向迭代器,rend、crend指向末尾的逆向迭代器。代码示例为:

std::string str = "abcd";
for (auto i = str.begin(); i != str.end(); ++i)
{
    std::cout << *i << " ";
}
std::cout << std::endl;

for (auto i = str.rbegin(); i != str.rend(); ++i)
{
    std::cout << *i << " ";
}
std::cout << std::endl;

输出结果为:

a b c d 
d c b a 

容量

empty

检查字符串是否为空,代码示例为:

std::string str;
std::cout << std::boolalpha;
std::cout << "str empty: " << str.empty() << std::endl;

str = "hello world";
std::cout << "str empty: " << str.empty() << std::endl;

str = "";
std::cout << "str empty: " << str.empty() << std::endl;

输出结果为:

str empty: true
str empty: false
str empty: true

size length

返回字符数,示例代码为:

std::string str = "hello world";
std::cout << "size = " << str.size() << std::endl;
std::cout << "length = " << str.length() << std::endl;

输出结果为:

size = 11
length = 11

max_size

返回可以保存的字符串最大数量。代码示例:

std::string str;
std::cout << "max_size = " << str.max_size() << std::endl;

输出结果为:

max_size = 9223372036854775791

capacity

返回当前已经分配的空间可以保存的字符数。代码示例:

std::string str = "hello world";
std::cout << "str size = " << str.size() << std::endl;
std::cout << "str capacity = " << str.capacity() << std::endl;

str = "";
std::cout << "str size = " << str.size() << std::endl;
std::cout << "str capacity = " << str.capacity() << std::endl;

输出结果为:

str size = 11
str capacity = 22
str size = 0
str capacity = 22

reserve

告诉basic_string对象根据requested_capacity重新分配空间,新分配的空间大小和requested_capacity没有关系。代码示例:

std::string str = "hello world";

str.reserve(50);
std::cout << "str capacity = " << str.capacity() << std::endl;

str.reserve(30);
std::cout << "str capacity = " << str.capacity() << std::endl;

可能的输出结果为:

str capacity = 63
str capacity = 63

shrink_to_fit

告诉basic_string减少空闲的空间,具体和编译器的实现有关。代码示例:

std::string str = "hello world";

str.reserve(50);
std::cout << "str capacity = " << str.capacity() << std::endl;

str.shrink_to_fit();
std::cout << "str capacity = " << str.capacity() << std::endl;

可能的输出结果为:

str capacity = 63
str capacity = 22

操作

clear

清除basic_string的所有字符。代码示例:

std::string str = "hello world";
std::cout << "Before clear, str = " << str << std::endl;
std::cout << "Before clear, str size = " << str.size() << std::endl;
std::cout << "Before clear, str capacity = " << str.capacity() << std::endl;

str.clear();
std::cout << "After clear, str = " << str << std::endl;
std::cout << "After clear, str size = " << str.size() << std::endl;
std::cout << "After clear, str capacity = " << str.capacity() << std::endl;

输出结果为:

Before clear, str = hello world
Before clear, str size = 11
Before clear, str capacity = 22
After clear, str = 
After clear, str size = 0
After clear, str capacity = 22

insert

在指定的位置插入字符串,插入的参数可以是字符、字符列表、迭代器、另一个basic_string或者basic_string_view。示例代码:

std::string origin_str = "hello_world";
std::string tmp = "12345";
std::string_view view = tmp;

std::string str1 = origin_str;
str1.insert(5, 3, 'a');
std::cout << "str1 = " << str1 << std::endl;

std::string str2 = origin_str;
str2.insert(5, "abcde");
std::cout << "str2 = " << str2 << std::endl;

std::string str3 = origin_str;
str3.insert(5, "abcde", 3);
std::cout << "str3 = " << str3 << std::endl;

std::string str4 = origin_str;
str4.insert(1, tmp);
std::cout << "str4 = " << str4 << std::endl;

std::string str5 = origin_str;
str5.insert(1, tmp, 1, 3);
std::cout << "str5 = " << str5 << std::endl;

std::string str6 = origin_str;
str6.insert(str6.begin() + 1, '4');
std::cout << "str6 = " << str6 << std::endl;

std::string str7 = origin_str;
str7.insert(str7.begin() + 1, 5, '4');
std::cout << "str7 = " << str7 << std::endl;

std::string str8 = origin_str;
str8.insert(str8.begin() + 1, tmp.begin() + 1, tmp.begin() + 3);
std::cout << "str8 = " << str8 << std::endl;

std::string str9 = origin_str;
str9.insert(str9.begin() + 2, {'3', '2', '1'});
std::cout << "str9 = " << str9 << std::endl;

std::string str10 = origin_str;
str10.insert(2, view);
std::cout << "str10 = " << str10 << std::endl;

std::string str11 = origin_str;
str11.insert(2, view, 1, 3);
std::cout << "str11 = " << str11 << std::endl;

输出结果为:

str1 = helloaaa_world
str2 = helloabcde_world
str3 = helloabc_world
str4 = h12345ello_world
str5 = h234ello_world
str6 = h4ello_world
str7 = h44444ello_world
str8 = h23ello_world
str9 = he321llo_world
str10 = he12345llo_world
str11 = he234llo_world

erase

移除指定位置的字符。代码示例:

std::string origin_str = "12345678";

std::string str1 = origin_str;
str1.erase(1, 3);
std::cout << "str1 = " << str1 << std::endl;

std::string str2 = origin_str;
str2.erase(str2.begin() + 2);
std::cout << "str2 = " << str2 << std::endl;

std::string str3 = origin_str;
str3.erase(str3.begin() + 2, str3.begin() + 5);
std::cout << "str3 = " << str3 << std::endl;

输出结果为:

str1 = 15678
str2 = 1245678
str3 = 12678

push_back

在末尾添加一个字符。代码示例:

std::string str = "1234";
std::cout << "str = " << str << std::endl;
str.push_back('a');
std::cout << "str = " << str << std::endl;

输出结果为:

str = 1234
str = 1234a

pop_back

移除末尾的字符,要求字符串不为空。代码示例:

std::string str = "1234";
std::cout << "str = " << str << std::endl;
str.pop_back();
std::cout << "str = " << str << std::endl;

输出结果为:

str = 1234
str = 123

append

在尾部添加字符串,添加的类型可以是字符、字符串、basic_string、basic_string_view、迭代器或者字符列表。代码示例:

std::string origin_str = "hello_world";
std::string tmp = "12345";
std::string_view view = tmp;

std::string str1 = origin_str;
str1.append(5, '1');
std::cout << "str1 = " << str1 << std::endl;

std::string str2 = origin_str;
str2.append(tmp);
std::cout << "str2 = " << str2 << std::endl;

std::string str3 = origin_str;
str3.append(tmp, 1, 2);
std::cout << "str3 = " << str3 << std::endl;

std::string str4 = origin_str;
str4.append("54321", 3);
std::cout << "str4 = " << str4 << std::endl;

std::string str5 = origin_str;
str5.append("54321");
std::cout << "str5 = " << str5 << std::endl;

std::string str6 = origin_str;
str6.append(tmp.begin() + 1, tmp.begin() + 3);
std::cout << "str6 = " << str6 << std::endl;

std::string str7 = origin_str;
str7.append({'c', 'b', 'a'});
std::cout << "str7 = " << str7 << std::endl;

std::string str8 = origin_str;
str8.append(view);
std::cout << "str8 = " << str8 << std::endl;

std::string str9 = origin_str;
str9.append(view, 1, 3);
std::cout << "str9 = " << str9 << std::endl;

输出结果为:

str1 = hello_world11111
str2 = hello_world12345
str3 = hello_world23
str4 = hello_world543
str5 = hello_world54321
str6 = hello_world23
str7 = hello_worldcba
str8 = hello_world12345
str9 = hello_world234

operator+=

在末尾添加字符串,可以添加的参数有字符、字符列表、字符串、basic_string或者basic_string_view。代码示例:

std::string origin_str = "hello_world";
std::string tmp = "12345";
std::string_view view = tmp;

std::string str1 = origin_str;
str1 += tmp;
std::cout << "str1 = " << str1 << std::endl;

std::string str2 = origin_str;
str2 += '1';
std::cout << "str2 = " << str2 << std::endl;

std::string str3 = origin_str;
str3 += "321";
std::cout << "str3 = " << str3 << std::endl;

std::string str4 = origin_str;
str4 += {'1', '2', '3'};
std::cout << "str4 = " << str4 << std::endl;

std::string str5 = origin_str;
str5 += view;
std::cout << "str5 = " << str5 << std::endl;

输出结果为:

str1 = hello_world12345
str2 = hello_world1
str3 = hello_world321
str4 = hello_world123
str5 = hello_world12345

compare

字符序列比较,可以传入的参数有字符串、basic_string以及basic_string_view。代码示例:

std::string str = "hello";
std::string_view view = str;
int ret = 0;

std::string str1 = "hello";
ret = str1.compare(str);
std::cout << "str1 compare str, ret = " << ret << std::endl;

std::string str2 = "hello1234";
ret = str2.compare(0, 5, str);
std::cout << "str2 compare str, ret = " << ret << std::endl;

std::string str3 = "hello1234";
ret = str3.compare(0, 5, str, 0, 4);
std::cout << "str3 compare str, ret = " << ret << std::endl;

std::string str4 = "hello";
ret = str4.compare("hello");
std::cout << "str4 compare str, ret = " << ret << std::endl;

std::string str5 = "hello1234";
ret = str5.compare(0, 5, "hello");
std::cout << "str5 compare str, ret = " << ret << std::endl;

std::string str6 = "hello1234";
ret = str6.compare(0, 5, "hello", 0, 4);
std::cout << "str6 compare str, ret = " << ret << std::endl;

std::string str7 = "hello";
ret = str7.compare(view);
std::cout << "str7 compare str, ret = " << ret << std::endl;

std::string str8 = "hello1234";
ret = str8.compare(0, 5, view);
std::cout << "str8 compare str, ret = " << ret << std::endl;

std::string str9 = "hello1234";
ret = str9.compare(0, 5, view, 0, 4);
std::cout << "str9 compare str, ret = " << ret << std::endl;

输出结果为:

str1 compare str, ret = 0
str2 compare str, ret = 0
str3 compare str, ret = 1
str4 compare str, ret = 0
str5 compare str, ret = 0
str6 compare str, ret = 1
str7 compare str, ret = 0
str8 compare str, ret = 0
str9 compare str, ret = 1

starts_with

todo

ends_with

todo

replace

替换指定位置的字符,可以用来替换的类型有字符、字符串、字符列表、迭代器、basic_string以及basic_string_view。代码示例:

std::string origin_str = "12345";
std::string replace_str = "abcde";
std::string replace_view = replace_str;

std::string str1 = origin_str;
str1.replace(1, 2, replace_str);
std::cout << "str1 = " << str1 << std::endl;

std::string str2 = origin_str;
str2.replace(str2.begin() + 1, str2.begin() + 4, replace_str);
std::cout << "str2 = " << str2 << std::endl;

std::string str3 = origin_str;
str3.replace(1, 2, replace_str, 1, 3);
std::cout << "str3 = " << str3 << std::endl;

std::string str4 = origin_str;
str4.replace(1, 2, "abcde", 4);
std::cout << "str4 = " << str4 << std::endl;

std::string str5 = origin_str;
str5.replace(str5.begin(), str5.begin() + 2, "abcde", 4);
std::cout << "str5 = " << str5 << std::endl;

std::string str6 = origin_str;
str6.replace(1, 2, "abcde");
std::cout << "str6 = " << str6 << std::endl;

std::string str7 = origin_str;
str7.replace(str7.begin(), str7.begin() + 2, "abcde");
std::cout << "str7 = " << str7 << std::endl;

std::string str8 = origin_str;
str8.replace(1, 2, 7, 'a');
std::cout << "str8 = " << str8 << std::endl;

std::string str9 = origin_str;
str9.replace(str9.begin() + 2, str9.begin() + 2, 7, 'a');
std::cout << "str9 = " << str9 << std::endl;

std::string str10 = origin_str;
str10.replace(str10.begin() + 1, str10.begin() + 3, replace_str.begin() + 1,
              replace_str.begin() + 4);
std::cout << "str10 = " << str10 << std::endl;

std::string str11 = origin_str;
str11.replace(str11.begin() + 1, str11.begin() + 3, {'a', 'b', 'c'});
std::cout << "str11 = " << str11 << std::endl;

std::string str12 = origin_str;
str12.replace(1, 2, replace_view);
std::cout << "str12 = " << str12 << std::endl;

std::string str13 = origin_str;
str13.replace(str13.begin() + 1, str13.begin() + 3, replace_view);
std::cout << "str13 = " << str13 << std::endl;

std::string str14 = origin_str;
str14.replace(1, 2, replace_view, 1, 3);
std::cout << "str14 = " << str14 << std::endl;

输出结果为:

str1 = 1abcde45
str2 = 1abcde5
str3 = 1bcd45
str4 = 1abcd45
str5 = abcd345
str6 = 1abcde45
str7 = abcde345
str8 = 1aaaaaaa45
str9 = 12aaaaaaa345
str10 = 1bcd45
str11 = 1abc45
str12 = 1abcde45
str13 = 1abcde45
str14 = 1bcd45

substr

返回字串:

basic_string substr(size_type pos, size_type count);

返回从位置pos开始指定长度count的子串。要求pos不能超过原字符串的长度。count可以超过字符串的界限,如果超过就返回pos后的整个子串。代码示例:

std::string str = "12345678";
std::string str1 = str.substr(1, 5);
std::cout << "str1 = " << str1 << std::endl;
std::string str2 = str.substr(1, 50);
std::cout << "str2 = " << str2 << std::endl;

输出结果为:

str1 = 23456
str2 = 2345678

copy

字符串拷贝:

size_type copy(value_type* dst, size_type count, size_type pos);

将字符串pos位置开始的count个字符拷贝到dst中。要要求pos的位置不能超过原字符串的长度。count可以超过字符串的界限,如果超过就拷贝pos后的整个子串。代码示例:

std::string str = "1234567";

char dst1[10] = {0};
str.copy(dst1, 5, 1);
std::cout << "dst1 = " << dst1 << std::endl;

char dst2[50] = {0};
str.copy(dst2, 50, 1);
std::cout << "dst2 = " << dst2 << std::endl;

输出结果为:

dst1 = 23456
dst2 = 234567

resize

重新指定字符串的长度,并且可以用指定的字符填充新增长度的字符。代码示例:

std::string str = "12345678";
str.resize(5);
std::cout << "str = " << str << ", size = " << str.size() << std::endl;
str.resize(8);
std::cout << "str = " << str << ", size = " << str.size() << std::endl;
str.resize(10, 'a');
std::cout << "str = " << str << ", size = " << str.size() << std::endl;

输出结果为:

str = 12345, size = 5
str = 12345, size = 8
str = 12345aa, size = 10

swap

交换两个basic_string中的字符串内容。代码示例:

std::string str1 = "12345678";
std::string str2 = "abc";
str1.swap(str2);
std::cout << "str1 = " << str1 << std::endl;
std::cout << "str2 = " << str2 << std::endl;

输出结果为:

str1 = abc
str2 = 12345678

npos

特殊值,通常用来表示不存在的位置。代码示例:

std::string::size_type n = std::string::npos;
std::cout << "npos = " << n << std::endl;

可能的输出结果为:

npos = 18446744073709551615

查找

find

从给定位置开始找到第一个和给定字符串相同的位置。代码示例:

std::string str = "12345678345678";
std::string find_str = "34";
std::string find_str_view = find_str;
std::string::size_type find_pos = 0;

find_pos = str.find(find_str, 4);
std::cout << "case1, find pos = " << find_pos << std::endl;
find_pos = str.find("345", 4, 2);
std::cout << "case2, find pos = " << find_pos << std::endl;
find_pos = str.find('3', 4);
std::cout << "case3, find pos = " << find_pos << std::endl;
find_pos = str.find(find_str_view, 9);
std::cout << "case4, find pos = " << find_pos << std::endl;

输出结果为:

case1, find pos = 8
case2, find pos = 8
case3, find pos = 8
case4, find pos = 18446744073709551615

rfind

找到给定位置之前的最后一个和给定字符串相同的位置。代码示例:

std::string str = "1112345678345678";
std::string find_str = "34";
std::string find_str_view = find_str;
std::string::size_type find_pos = 0;

find_pos = str.rfind(find_str, 12);
std::cout << "case1, find pos = " << find_pos << std::endl;
find_pos = str.rfind("345", 7, 2);
std::cout << "case2, find pos = " << find_pos << std::endl;
find_pos = str.rfind('3', 7);
std::cout << "case3, find pos = " << find_pos << std::endl;
find_pos = str.rfind(find_str_view, 3);
std::cout << "case4, find pos = " << find_pos << std::endl;

输出结果为:

case1, find pos = 10
case2, find pos = 4
case3, find pos = 4
case4, find pos = 18446744073709551615

find_first_of

从给定位置后找到首个等于给定字符串中的某个字符的位置。代码示例:

std::string str = "123456783541122345678";
std::string find_str = "543";
std::string find_str_view = find_str;
std::string::size_type find_pos = 0;

find_pos = str.find_first_of(find_str, 7);
std::cout << "case1, find pos = " << find_pos << std::endl;
find_pos = str.find_first_of("54321", 7, 3);
std::cout << "case2, find pos = " << find_pos << std::endl;
find_pos = str.find_first_of("543", 7);
std::cout << "case3, find pos = " << find_pos << std::endl;
find_pos = str.find_first_of('3', 7);
std::cout << "case4, find pos = " << find_pos << std::endl;
find_pos = str.find_first_of(find_str_view, 7);
std::cout << "case5, find pos = " << find_pos << std::endl;

输出结果为:

case1, find pos = 8
case2, find pos = 8
case3, find pos = 8
case4, find pos = 8
case5, find pos = 8

find_first_not_of

从给定位置后找到首个不等于给定字符串中的任何一个字符的位置。代码示例:

std::string str = "123451234512345123454321";
std::string find_str = "1234";
std::string find_str_view = find_str;
std::string::size_type find_pos = 0;

find_pos = str.find_first_not_of(find_str, 5);
std::cout << "case1, find pos = " << find_pos << std::endl;
find_pos = str.find_first_not_of("12345", 5, 4);
std::cout << "case2, find pos = " << find_pos << std::endl;
find_pos = str.find_first_not_of("1234", 5);
std::cout << "case3, find pos = " << find_pos << std::endl;
find_pos = str.find_first_not_of('3', 4);
std::cout << "case4, find pos = " << find_pos << std::endl;
find_pos = str.find_first_not_of(find_str_view, 5);
std::cout << "case5, find pos = " << find_pos << std::endl;

输出结果为:

case1, find pos = 9
case2, find pos = 9
case3, find pos = 9
case4, find pos = 4
case5, find pos = 9

find_last_of

从给定位置前找到最后一个等于给定字符串中的某个字符的位置。代码示例:

std::string str = "123456783541122345678";
std::string find_str = "543";
std::string find_str_view = find_str;
std::string::size_type find_pos = 0;

find_pos = str.find_last_of(find_str, 14);
std::cout << "case1, find pos = " << find_pos << std::endl;
find_pos = str.find_last_of("54321", 14, 3);
std::cout << "case2, find pos = " << find_pos << std::endl;
find_pos = str.find_last_of("543", 14);
std::cout << "case3, find pos = " << find_pos << std::endl;
find_pos = str.find_last_of('3', 14);
std::cout << "case4, find pos = " << find_pos << std::endl;
find_pos = str.find_last_of(find_str_view, 14);
std::cout << "case5, find pos = " << find_pos << std::endl;

输出结果为:

case1, find pos = 10
case2, find pos = 10
case3, find pos = 10
case4, find pos = 8
case5, find pos = 10

find_last_not_of

从给定位置前找到最后一个不等于给定字符串中的任何一个字符的位置。代码示例:

std::string str = "123451234512345123454321";
std::string find_str = "1234";
std::string find_str_view = find_str;
std::string::size_type find_pos = 0;

find_pos = str.find_last_not_of(find_str, 18);
std::cout << "case1, find pos = " << find_pos << std::endl;
find_pos = str.find_last_not_of("12345", 18, 4);
std::cout << "case2, find pos = " << find_pos << std::endl;
find_pos = str.find_last_not_of("1234", 18);
std::cout << "case3, find pos = " << find_pos << std::endl;
find_pos = str.find_last_not_of('3', 18);
std::cout << "case4, find pos = " << find_pos << std::endl;
find_pos = str.find_last_not_of(find_str_view, 18);
std::cout << "case5, find pos = " << find_pos << std::endl;

输出结果为:

case1, find pos = 14
case2, find pos = 14
case3, find pos = 14
case4, find pos = 18
case5, find pos = 14

运算符

operator+

basic_string可以和字符、字符串或者basic_string相加得到新的basic_string。代码示例:

std::string tmp1 = "111";
std::string tmp2 = "222";

std::string str1 = tmp1 + tmp2;
std::cout << "str1 = " << str1 << std::endl;
std::string str2 = tmp1 + "aaa";
std::cout << "str2 = " << str2 << std::endl;
std::string str3 = tmp1 + 'a';
std::cout << "str3 = " << str3 << std::endl;
std::string str4 = "aaa" + tmp1;
std::cout << "str4 = " << str4 << std::endl;
std::string str5 = 'a' + tmp1;
std::cout << "str5 = " << str5 << std::endl;

输出结果为:

str1 = 111222
str2 = 111aaa
str3 = 111a
str4 = aaa111
str5 = a111

比较运算符

operator==、!=、<、<=、>、>=用于basic_string和字符串或者basic_string的比较。代码示例:

std::string tmp1 = "111";
std::string tmp2 = "222";

bool ret = false;
std::cout << std::boolalpha;
ret = tmp1 == tmp2;
std::cout << "case1: " << ret << std::endl;
ret = tmp1 == "222";
std::cout << "case2: " << ret << std::endl;

输出结果为:

case1: false
case2: false

输入输出

operator<<、>>

用于字符串的流输入与输出。代码示例:

std::string str = "111 aaa";
std::istringstream iss(str);

std::string str1;
std::string str2;
iss >> str1;
iss >> str2;

std::cout << "str1 = " << str1 << std::endl;
std::cout << "str2 = " << str2 << std::endl;

输出结果为:

str1 = 111
str2 = aaa

getline

从输入流中读取读取字符并放进字符串,delim是分隔符,默认的分隔符是'\n',即换行符:

getline(basic_istream& input, basic_string& str, CharT delim);
getline(basic_istream& input, basic_string& str);

代码示例:

std::istringstream input1;
input1.str("abc; 123;ab1");
for (std::string line; std::getline(input1, line, ';');)
{
    std::cout << "line is:" << line << std::endl;
}
std::cout << std::endl;

std::istringstream input2;
input2.str("abc\n 123\nab1");
for (std::string line; std::getline(input2, line);)
{
    std::cout << "line is:" << line << std::endl;
}

输出结果为:

line is:abc
line is: 123
line is:ab1

line is:abc
line is: 123
line is:ab1

数值转换

stoi、stol、stoll

将字符串转换为整数:

int stoi  (const string& str, size_t* pos, int base);

从首个非空字符开始,把连续的数字字符组成以base为基底的整数,pos是首个非空字符后的第一个不是数字字符的位置。如果首个非空字符不是数字,那么将抛出错误 。代码示例:

std::string s1 = "2341 #abc";
std::string s2 = "abc2341 #abc";
std::size_t pos;
int num = 0;

num = std::stoi(s1, &pos, 10);
std::cout << "base 10, pos = " << pos << ", num = " << num << std::endl;
num = std::stoi(s1, &pos, 8);
std::cout << "base 8,  pos = " << pos << ", num = " << num << std::endl;
num = std::stoi(s1, &pos, 16);
std::cout << "base 16, pos = " << pos << ", num = " << num << std::endl;

try
{
    num = std::stoi(s2, &pos, 10);
}
catch (std::invalid_argument const& ex)
{
    std::cout << ex.what() << std::endl;
}

输出结果为:

base 10, pos = 4, num = 2341
base 8,  pos = 4, num = 1249
base 16, pos = 4, num = 9025
stoi: no conversion

stoul、stoull

将字符串转换为无符号整数。

stof、stod、stold

将字符串转换为浮点数。代码示例:

std::string s = "2341.1234#abc";
std::size_t pos;
float num = std::stof(s, &pos);
printf("num = %.8f\n", num);

输出结果为:

num = 2341.12329102

to_string、to_wstring

将整数或者浮点数转换为basic_string类型。代码示例:

int num1 = 10;
float num2 = 23.34;
double num3 = 1e-7;
double num4 = 1e40;
double num5 = 12345678;

std::cout << "num1 = " << std::to_string(num1) << std::endl;
std::cout << "num2 = " << std::to_string(num2) << std::endl;
std::cout << "num3 = " << std::to_string(num3) << std::endl;
std::cout << "num4 = " << std::to_string(num4) << std::endl;
std::cout << "num5 = " << std::to_string(num5) << std::endl;

输出结果为:

num1 = 10
num2 = 23.340000
num3 = 0.000000
num4 = 10000000000000000303786028427003666890752.000000
num5 = 12345678.000000

字面量

创建所需类型的basic_string字面量。代码示例:

using namespace std::string_literals;

std::string s1 = "abc\0\0def";
std::string s2 = "abc\0\0def"s;
std::cout << "s1 = " << s1 << std::endl;
std::cout << "s2 = " << s2 << std::endl;

输出结果为:

s1 = abc
s2 = abcdef

其他

std::hash

支持对basic_string类型的散列表。代码示例:

std::string str = "hello world";
std::string_view str_view = str;
std::cout << std::hash{}(str) << std::endl;
std::cout << std::hash{}(str_view) << std::endl;

输出结果为:

12386028635079221413
12386028635079221413

std::swap

交换两个basic_string保存的字符串内容。代码示例:

std::string str1 = "aaa";
std::string str2 = "111";
std::swap(str1, str2);
std::cout << "str1 = " << str1 << std::endl;
std::cout << "str2 = " << str2 << std::endl;

输出结果为:

str1 = 111
str2 = aaa

你可能感兴趣的:(c++标准库基础,c++,开发语言)