【C++】STL之string 超详解

目录

1.string概述

 2.string使用

1.构造初始化

2.成员函数 

1.迭代器

2.容量操作

 1.size和length   返回字符串长度

2.resize   调整字符串大小

3.capacity   获得字符串容量

4.reserve  调整容量

5.clear  清除

6.empty   判空

3.string插入、追加  、拼接

1.运算符+=  、append

2. push_back

3.insert 

 4.string删除、替换

1.erase

2.pop_back

3. replace

5.字符串操作

1.c_stl

2.data

 3.copy

4.find、rfind

5.find_first_of、find_last_of

6.substr    子串处理

3.string反转(reverse)

4.非成员函数 

1.getline(读取)

2.swap(交换)

3. relational operators(比较)

5.访问遍历 

 6.静态成员常量(npos)


1.string概述

C++的标准库中定义了string类

1. string是表示字符串的字符串类  

2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作

下面是C++的官方网站,若有疑问,可在网站中查询学习 

string - C++ Reference (cplusplus.com)icon-default.png?t=N7T8https://legacy.cplusplus.com/reference/string/string/

 2.string使用

1.构造初始化

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1;

	string s2("hello world");

	string s3 = s2;

	string s4(s2);

	string s5(s2, 1, 6);//用另一个字符串的区间构造
	cout << s5 << endl;

	string s6(s2, 1);//用另一个字符串的区间构造
	cout << s6 << endl;

	string s7(s2, 1, 100);//用另一个字符串的区间构造
	cout << s7 << endl;

	string s8("hello world", 5);//用另一个字符串的区间构造,且只拷贝构造前5个字符
	cout << s8 << endl;

	string s9(10, 'x');//单字符构造,构造十个字符
	cout << s9 << endl;

	//赋值运算符重载构造
	s1 = s2;

	s1 = "world";

	s1 = 'x';

	return 0;
}
ello w
ello world
ello world
hello
xxxxxxxxxx

2.成员函数 

1.迭代器

迭代器是一种可以统一使用的遍历方式,迭代器类似于C里面的指针

我们可以使用 begin()end() 函数来获取字符串的起始和结束,除了使用迭代器来遍历字符串,我们还可以使用迭代器来进行插入、删除等操作

begin和end是正向迭代器,rbegin和rend是反向迭代器

具体操作请看目录 5.访问遍历

2.容量操作

C++的string类提供了许多与字符串容量相关的接口

 1.size和length   返回字符串长度

两者作用相同,都是返回字符串长度,平常推荐用size,其他容器也是用size

string s1("hello world");
cout << s1.size() << endl;
cout << s1.length() << endl;
2.resize   调整字符串大小

 resize调整字符串的大小。该方法可以接收一个参数,用于指定字符串的新大小。如果新大小小于当前大小,则丢弃超出新大小的字符;如果新大小大于当前大小,则在末尾添加空字符或指定的字符。

 string str = "Hello";
 // 调整字符串为 10 个字符,并在末尾添加 '!'
 str.resize(10, '!');
 cout << str << endl;
 // 调整字符串为 3 个字符,丢弃超出的字符
 str.resize(3);

resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字 符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的 元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大 小,如果是将元素个数减少,底层空间总大小不变。 

3.capacity   获得字符串容量

获取字符串的容量

string str = "Hello";
cout << str.capacity() << endl;

需要注意的是,字符串的容量并不一定等于字符串的长度(即字符的个数)。容量表示的是在不重新分配内存的情况下,字符串所能容纳的最大字符数。当字符串的长度超过当前容量时,可能会触发重新分配内存的操作。

4.reserve  调整容量

预留字符串的存储空间,以避免频繁的内存重新分配操作

string str = "Hello";

// 预留至少 20 个字符的存储空间
str.reserve(20);

// 获取字符串的容量
cout << "Capacity after reserving space: " << str.capacity() << endl;

通过预留存储空间,我们可以避免频繁的内存重新分配操作,从而提高程序的性能和效率。当我们预先知道要存储的字符串较大时,预留足够的存储空间可以减少内存分配的次数,节省时间和资源。需要注意的是,reserve() 方法只会增加字符串的容量,而不会改变字符串的长度。 

5.clear  清除

清空字符串中的内容,使其成为空字符串

string str = "Hello";
// 清空字符串的内容
str.clear();

通过使用clear()方法,我们可以方便地清空字符串的内容,使其变为空字符串。这在需要重复使用同一个字符串对象时非常有用,可以避免反复创建新的字符串对象,节省内存和提高效率。

6.empty   判空

判断字符串是否为空,即字符串的长度是否为 0。

empty() 方法返回一个布尔值,表示字符串是否为空。如果字符串为空,则返回 true;否则返回 false。 

string str = "Hello";
// 判断字符串是否为空
if (str.empty()) {
    cout << "The string is empty." << endl;
}
else {
    cout << "The string is not empty." << endl;
}

3.string插入、追加  、拼接

1.运算符+=  、append

实现字符串的拼接

 string str = "Hello";
 // 使用运算符+=,append拼接新的字符串
 str += " World!";
 str.append(" World!");

 无论是使用运算符+=还是append()方法,都可以实现字符串的拼接。选择使用哪种方式取决于个人偏好和代码的可读性

2. push_back

向字符串末尾添加一个新的字符

string str = "Hello";
// 向字符串末尾添加新的字符
str.push_back('!');
3.insert 

在字符串的指定位置之前插入字符、子字符串或其他字符串

basic_string& insert(size_type pos, const basic_string& str);
basic_string& insert(size_type pos, const basic_string& str, size_type pos1, size_type n);
basic_string& insert(size_type pos, const CharT* cstr);
basic_string& insert(size_type pos, const CharT* cstr, size_type n);
basic_string& insert(size_type pos, size_type n, CharT c);
iterator insert(const_iterator p, size_type n, CharT c);
template<class InputIterator>
iterator insert(const_iterator p, InputIterator first, InputIterator last);
string str = "Hello world";
// 在位置 5 插入一个空格
str.insert(5, " ");
// 在位置 6 插入子字符串 "beautiful"
str.insert(6, "beautiful", 0, 9);

 注意,该操作时间复杂度较高,应少用

 4.string删除、替换

1.erase

删除字符串中的字符或子字符串

basic_string& erase(size_type pos = 0, size_type n = npos);
iterator erase(const_iterator p);
iterator erase(const_iterator first, const_iterator last);

参数 pos 表示要删除的起始位置,参数 n 表示要删除的字符数。通过 erase() 方法,可以删除指定位置或指定区间的字符或子字符

string str = "Hello world";
// 删除位置 5 开始的 6 个字符
str.erase(5, 6);
// 删除最后一个字符
str.erase(str.end() - 1);
2.pop_back

删除最后一个字符

string str = "Hello";
// 删除最后一个字符
str.pop_back();
3. replace

替换字符串中的字符或子字符串

basic_string& replace(size_type pos, size_type count, const basic_string& str);
basic_string& replace(const_iterator first, const_iterator last, const basic_string& str);
basic_string& replace(size_type pos, size_type count, const basic_string& str, size_type pos2, size_type count2);
basic_string& replace(size_type pos, size_type count, const CharT* cstr, size_type count2);
basic_string& replace(const_iterator first, const_iterator last, const CharT* cstr, size_type count2);
basic_string& replace(size_type pos, size_type count, const CharT* cstr);
basic_string& replace(const_iterator first, const_iterator last, const CharT* cstr);
basic_string& replace(size_type pos, size_type count, size_type count2, CharT ch);
basic_string& replace(const_iterator first, const_iterator last, size_type count2, CharT ch);
template<class InputIterator>
basic_string& replace(const_iterator first, const_iterator last, InputIterator first2, InputIterator last2);

参数 pos 表示要替换的起始位置,参数 count 表示要替换的字符数,参数 str 表示替换后的新字符串,参数 cstr 表示以空字符结尾的 C 风格字符串,参数 count2 表示要替换的字符数,参数 ch 表示要替换的单个字符。通过 replace() 方法,可以在指定位置替换字符或子字符串。 

string str = "Hello world";

// 替换位置 6 开始的 5 个字符为 "beautiful"
str.replace(6, 5, "beautiful");

5.字符串操作

1.c_stl

获取字符串对象的以空字符结尾的 C 风格字符串。

const char* c_str() const noexcept;

通过调用 c_str() 方法,可以获取一个指向字符串对象的以空字符结尾的 C 风格字符串的指针。这个指针可以用于与需要 C 风格字符串作为参数的函数进行交互,或者直接打印输出字符串。

string str = "Hello";
// 获取 C 风格字符串指针
const char* cstr = str.c_str();

需要注意的是,通过 c_str() 方法获取的 C 风格字符串指针是只读的,不应该修改其中的内容。同时,在字符串对象生命周期结束之前,这个指针是有效的,但在字符串对象发生改变(如进行增删改操作)时,这个指针可能会变得无效。因此,在使用 c_str() 方法获取的指针时,应该注意有效性和不可修改性。

2.data

获取字符串对象的数据存储区域的指针。

const char* data() const noexcept;

通过调用 data() 方法,可以直接获取一个指向字符串对象数据存储区域的指针。这个指针可以用于与需要字符数组作为参数的函数进行交互,或者直接访问和修改字符串的内容。

需要注意的是,通过 data() 方法获取的指针并不保证以空字符结尾,因此在使用时应该谨慎处理。如果需要获取以空字符结尾的 C 风格字符串,可以使用 c_str() 方法。

string str = "Hello";

// 获取数据存储区域指针
const char* pdata = str.data();

// 修改第一个字符为 'h'
*pdata = 'h';
 3.copy

通过调用 copy() 方法,可以将字符串对象中的字符复制到指定的目标字符数组中

string str = "Hello";
char dest[10];

// 复制字符串到目标字符数组
str.copy(dest, 5);

// 在目标字符数组末尾添加空字符
dest[5] = '\0';
4.find、rfind

查找一个子串

size_t find(const std::string& str, size_t pos = 0) const noexcept;

 通过调用 find() 方法,可以在字符串对象中查找指定的子串,并返回子串第一次出现的位置。参数 str 是要查找的子串,pos 是查找的起始位置,默认为 0。

string str = "Hello, world!";

// 查找子串 ", "
size_t pos = str.find(", ");

// 如果找到,则截取子串前面的部分并打印输出
if (pos != std::string::npos) {
    cout << str.substr(0, pos) << std::endl;  // 输出 "Hello"
}

需要注意的是,如果 find() 方法没有找到指定的子串,它会返回 std::string::npos,这是一个常量,表示未找到。因此,在判断是否找到了子串时,应该使用 !=std::string::npos 的形式。 

 rfind() 方法和 find() 方法类似,但是它是从字符串的末尾开始查找子串或字符。

5.find_first_of、find_last_of

在字符串中查找参数中任何一个字符第一次出现的位置

size_t find_first_of(const std::string& str, size_t pos = 0) const noexcept;
size_t find_first_of(const char* s, size_t pos, size_t n) const noexcept;
size_t find_first_of(char c, size_t pos = 0) const noexcept;

可以传递一个字符串、一个字符数组或一个字符作为参数。这些函数会返回参数中任何一个字符第一次出现的位置,如果未找到则返回 std::string::npos。 

string str = "Hello, world!";
// 查找参数中任何一个字符第一次出现的位置
size_t pos1 = str.find_first_of("aeiou");
if (pos1 != std::string::npos) {
    cout << "First vowel found at position: " << pos1 << endl;
}
else {
    cout << "No vowels found" << endl;
}

// 从指定位置开始查找参数中任何一个字符第一次出现的位置
size_t pos2 = str.find_first_of("aeiou", pos1 + 1);
if (pos2 != std::string::npos) {
    cout << "Next vowel found at position: " << pos2 << endl;
}
else {
    cout << "No more vowels found" << endl;
}

在这个示例中,我们首先使用 find_first_of 查找字符串中第一个元音字母的位置,然后从该位置的下一个字符开始继续查找下一个元音字母的位置。 

find_last_of与fi_first_of类似,用于在字符串中查找参数中任何一个字符最后一次出现的位置。

6.substr    子串处理

从当前字符串中提取子字符串

td::string substr(size_t pos = 0, size_t count = std::string::npos) const;
  • pos 参数指定要提取的子字符串的起始位置,默认为 0。
  • count 参数指定要提取的字符数目,默认为 std::string::npos,表示提取从起始位置到字符串末尾的所有字符。
string str = "Hello, world!";

// 提取从第7个字符开始的子字符串
string sub1 = str.substr(6);
std::cout << "Substring starting from position 6: " << sub1 << endl;

// 提取从第7个字符开始的3个字符
string sub2 = str.substr(6, 3);
cout << "Substring of length 3 starting from position 6: " << sub2 << endl;

3.string反转(reverse)

在 C++ 中,可以通过使用 std::reverse 算法来反转容器(比如 std::stringstd::vector 等)中的元素。

string str = "Hello, world!";

// 反转字符串
reverse(str.begin(), str.end());

4.非成员函数 

1.getline(读取)

用于从输入流中读取一行文本的函数

cin不能读取带空格的字符串,getline可以

 std::string input;

    // 从标准输入中读取一行文本
    std::cout << "Please enter a line of text: ";
    std::getline(std::cin, input);

    // 输出读取的文本
    std::cout << "You entered: " << input << std::endl;
2.swap(交换)

用于交换两个字符串的内容。这个函数会以常数时间完成字符串的交换,因此比使用复制和赋值运算符更有效率。

string str1 = "Hello";
string str2 = "World";

// 使用 std::string::swap 交换 str1 和 str2 的内容
str1.swap(str2);
3. relational operators(比较)

在 C++ 中,字符串 std::string 类型支持使用关系运算符(relational operators)进行比较操作。这些关系运算符可以用于比较两个字符串对象之间的大小关系,例如判断两个字符串是否相等、大小关系等。

以下是 std::string 类型支持的关系运算符:

  1. ==:检查两个字符串是否相等。
  2. !=:检查两个字符串是否不相等。
  3. <:检查一个字符串是否小于另一个字符串。
  4. >:检查一个字符串是否大于另一个字符串。
  5. <=:检查一个字符串是否小于或等于另一个字符串。
  6. >=:检查一个字符串是否大于或等于另一个字符串。

这些关系运算符可以在比较字符串时非常有用。例如,你可以使用这些运算符来对字符串进行字典序的比较,或者在排序算法中对字符串进行排序。

  std::string str1 = "apple";
    std::string str2 = "banana";

    if (str1 == str2) {
        std::cout << "str1 is equal to str2" << std::endl;
    } else if (str1 < str2) {
        std::cout << "str1 is less than str2" << std::endl;
    } else {
        std::cout << "str1 is greater than str2" << std::endl;
    }

5.访问遍历 

string的遍历有三种方式:1.下标 2.迭代器 3.范围for 

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1("hello world");
	//1.下标
	for (size_t i = 0; i < s1.size(); i++)
		{
			cout << s1[i] << " ";
		}
	//2.迭代器
	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		cout << *it << " ";
		++it;
	}
	//3.范围for
	for (auto e : s1)
	{
		cout << e << " ";
	}
	return 0;
}

 6.静态成员常量(npos)

nposstd::string 类型中的一个静态成员常量,它通常被用来表示字符串中没有匹配项或者没有找到所寻找的子串的位置。npos 通常被定义为 std::string::npos,其值为一个特定的整数(通常是 -1 或者是一个很大的正整数),这个整数的值可以保证不会和有效的字符串下标产生冲突

举个例子,当你在字符串中查找某个子串时,如果没有找到这个子串,则返回的位置是 std::string::npos。在 std::string::find() 函数中,如果找到了子串,那么它会返回子串在字符串中的位置,否则会返回 std::string::npos

                                   以上就是本篇文章的全部内容,谢谢观看

  • 22
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

饿了我会自己捡代码吃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值