【C++初阶】C++STL详解(一)—— string类(下)

简介: 【C++初阶】C++STL详解(一)—— string类(下)

11.string中运算符的使用

1、operator=

 string类中对=运算符进行了重载,重载后的=运算符支持string类的赋值、字符串的赋值以及字符的赋值。

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string s1;
  string s2("CSDN");
  //支持string类的赋值
  s1 = s2;
  cout << s1 << endl; //CSDN
  //支持字符串的赋值
  s1 = "hello";
  cout << s1 << endl;  //hello
  //支持字符的赋值
  s1 = 'x';
  cout << s1 << endl; //x
  return 0;
}

2、operator+=

 string类中对+=运算符进行了重载,重载后的+=运算符支持string类的复合赋值、字符串的复合赋值以及字符复合的赋值。

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string s1;
  string s2("hello");
  //支持string类的复合赋值
  s1 += s2;
  cout << s1 << endl; //hello
  //支持字符串的复合赋值
  s1 += " CSDN";
  cout << s1 << endl; //hello CSDN
  //支持字符的复合赋值
  s1 += '!';
  cout << s1 << endl; //hello CSDN!
  return 0;
}

其实,operator+=的底层就是调用的push_back和append

3、operator+

string类中对+运算符进行了重载,重载后的+运算符支持以下几种类型的操作:

 string类 + string类

 string类 + 字符串

 字符串 + string类

 string类 + 字符

 字符 + string类

它们相加后均返回一个string类对象。

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string s;
  string s1("super");
  string s2("man");
  char str[] = "woman";
  char ch = '!';
  //string类 + string类
  s = s1 + s2;
  cout << s << endl; //superman
  //string类 + 字符串
  s = s1 + str;
  cout << s << endl; //superwoman
  //字符串 + string类
  s = str + s1;
  cout << s << endl; //womansuper
  //string类 + 字符
  s = s1 + ch;
  cout << s << endl; //super!
  //字符 + string类
  s = ch + s1;
  cout << s << endl; //!super
  return 0;
}

4、operator>> 和 operator<<

 string类中也对>>和<<运算符进行了重载,这就是为什么我们可以直接使用>>和<<对string类进行输入和输出的原因。


istream& operator>> (istream& is, string& str);

ostream& operator<< (ostream& os, const string& str);

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string s;
  cin >> s; //输入
  cout << s << endl; //输出
  return 0;
}

5、!=、<、<=、>等

 string类中还对一系列关系运算符进行了重载,它们分别是==、!=、<、<=、>、>=。重载后的关系运算符支持string类和string类之间的关系比较、string类和字符串之间的关系比较、字符串和string类之间的关系比较。

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string s1("abcd");
  string s2("abde");
  cout << (s1 > s2) << endl; //0
  cout << (s1 < s2) << endl; //1
  cout << (s1 == s2) << endl; //0
  return 0;
}

注意:这些重载的关系比较运算符所比较的都是对应字符的ASCII码值

12.string中与迭代器相关的函数


1、与正向迭代器相关的函数

begin函数:返回一个指向字符串第一个字符的迭代器。

函数原型:

iterator begin();

 const_iterator begin() const;

end函数:返回一个指向字符串结束字符的迭代器,即’\0’。

函数原型:

iterator end();

 const_iterator end() const;

使用示例:

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string s("hello");
  //正向迭代器
  string::iterator it = s.begin();
  while (it != s.end())
  {
    cout << *it;
    it++;
  }
  cout << endl; //hello
  return 0;
}

image.png2、与反向迭代器相关的函数

rbegin函数:返回指向字符串最后一个字符的反向迭代器。

函数原型:

reverse_iterator rbegin();

 const_reverse_iterator rbegin() const;

rend函数:返回指向字符串第一个字符前面的理论元素的反向迭代器。

函数原型:

reverse_iterator rend();

 const_reverse_iterator rend() const;

使用示例:

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string s("hello");
  //反向迭代器
  string::reverse_iterator rit = s.rbegin();
  while (rit != s.rend())
  {
    cout << *rit;
    rit++;
  }
  cout << endl; //olleh
  return 0;
}

f6fc2f619e7c4189b248a6979585dade.png

13.string与字符串之间的转换

1、将字符串转换为string

 将字符串转换为string很简单,在前面讲string的定义方式时就有说到。

#include <iostream>
#include <string>
using namespace std;
int main()
{
  //方式一
  string s1("hello world");
  //方式二
  char str[] = "hello world";
  string s2(str);
  cout << s1 << endl; //hello world
  cout << s2 << endl; //hello world
  return 0;
}

2、使用c_str或data将string转换为字符串

const char* c_str() const;

const char* data() const;

区别:

在C++98中,c_str()返回 const char* 类型,返回的字符串会以空字符结尾。

在C++98中,data()返回 const char* 类型,返回的字符串不以空字符结尾。

但是在C++11版本中,c_str()与data()用法相同。

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string s("hello world ");
  const char* str1 = s.data();
  const char* str2 = s.c_str();
  cout << str1 << endl;
  cout << str2 << endl;
  return 0;
}

兼容C语言,相互配合:f87071df951e485995b7ae74970169a5.pngC语言与C++中字符串读取结束的区别:image.png

14.string中子字符串的提取

1、使用substr函数提取string中的子字符串

string substr (size_t pos = 0, size_t len = npos) const;

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string ur1 = "https://cplusplus.com/reference/string/string/";
  size_t pos1 = ur1.find("://");
  string protocol;//协议
  if (pos1 != string::npos)//找到了
  {
    protocol = ur1.substr(0, pos1);//substr(pos, n)提取pos位置开始的n个字符序列作为返回值
  }
  cout << protocol << endl;
  string domain;//域名
  string uri;//资源名
  size_t pos2 = ur1.find("/", pos1 + 3);
  if (pos1 != string::npos)//找到了
  {
    domain = ur1.substr(pos1 + 3, pos2 - (pos1 + 3));
    uri = ur1.substr(pos2 + 1);
  }
  cout << domain << endl;
  cout << uri << endl;
  return 0;
}

f5c9829688a5445baedbeac53cd7a350.png2、使用copy函数将string的子字符串复制到字符数组中

size_t copy (char* s, size_t len, size_t pos = 0) const;

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string s("abcdef");
  char str[20];
  //copy(str, n, pos)复制pos位置开始的n个字符到str字符串
  size_t length = s.copy(str, 4, 2);
  //copy函数不会在复制内容的末尾附加'\0',需要手动加
  str[length] = '\0';
  cout << str << endl; //dcef
  return 0;
}

15.string中的getline函数

在使用>>进行输入操作时,当>>读取到空格便会停止读取,基于此,我们将不能用>>将一串含有空格的字符串读入到string对象中。

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string s;
  cin >> s; //输入:hello CSDN
  cout << s << endl; //输出:hello
  return 0;
}

这时,我们就需要用getline函数完成一串含有空格的字符串的读取操作了。

用法一:

istream& getline (istream& is, string& str);

getline函数将从is中提取到的字符存储到str中,直到读取到换行符’\n’为止。

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string s;
  getline(cin, s); //输入:hello CSDN
  cout << s << endl; //输出:hello CSDN
  return 0;
}

用法二:

istream& getline (istream& is, string& str, char delim);

getline函数将从is中提取到的字符存储到str中,直到读取到分隔符delim或换行符’\n’为止。

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string s;
  getline(cin, s, 'D'); //输入:hello CSDN
  cout << s << endl; //输出:hello CS
  return 0;
}

16.总结:

今天我们认识并具体学习了STL中string类,开启了STL学习的大门。接下来,我们将进行STL中string类的模拟实现。希望我的文章和讲解能对大家的学习提供一些帮助。

当然,本文仍有许多不足之处,欢迎各位小伙伴们随时私信交流、批评指正!我们下期见~

c3ad96b16d2e46119dd2b9357f295e3f.jpg

相关文章
|
2天前
|
C++
模拟实现c++中的string
模拟实现c++中的string
|
24天前
|
C++ 容器
【c++丨STL】stack和queue的使用及模拟实现
本文介绍了STL中的两个重要容器适配器:栈(stack)和队列(queue)。容器适配器是在已有容器基础上添加新特性或功能的结构,如栈基于顺序表或链表限制操作实现。文章详细讲解了stack和queue的主要成员函数(empty、size、top/front/back、push/pop、swap),并提供了使用示例和模拟实现代码。通过这些内容,读者可以更好地理解这两种数据结构的工作原理及其实现方法。最后,作者鼓励读者点赞支持。 总结:本文深入浅出地讲解了STL中stack和queue的使用方法及其模拟实现,帮助读者掌握这两种容器适配器的特性和应用场景。
55 21
|
2月前
|
编译器 C语言 C++
【c++丨STL】list模拟实现(附源码)
本文介绍了如何模拟实现C++中的`list`容器。`list`底层采用双向带头循环链表结构,相较于`vector`和`string`更为复杂。文章首先回顾了`list`的基本结构和常用接口,然后详细讲解了节点、迭代器及容器的实现过程。 最终,通过这些步骤,我们成功模拟实现了`list`容器的功能。文章最后提供了完整的代码实现,并简要总结了实现过程中的关键点。 如果你对双向链表或`list`的底层实现感兴趣,建议先掌握相关基础知识后再阅读本文,以便更好地理解内容。
42 1
|
2月前
|
算法 C语言 C++
【c++丨STL】list的使用
本文介绍了STL容器`list`的使用方法及其主要功能。`list`是一种双向链表结构,适用于频繁的插入和删除操作。文章详细讲解了`list`的构造函数、析构函数、赋值重载、迭代器、容量接口、元素访问接口、增删查改操作以及一些特有的操作接口如`splice`、`remove_if`、`unique`、`merge`、`sort`和`reverse`。通过示例代码,读者可以更好地理解如何使用这些接口。最后,作者总结了`list`的特点和适用场景,并预告了后续关于`list`模拟实现的文章。
69 7
|
3月前
|
存储 编译器 C语言
【c++丨STL】vector的使用
本文介绍了C++ STL中的`vector`容器,包括其基本概念、主要接口及其使用方法。`vector`是一种动态数组,能够根据需要自动调整大小,提供了丰富的操作接口,如增删查改等。文章详细解释了`vector`的构造函数、赋值运算符、容量接口、迭代器接口、元素访问接口以及一些常用的增删操作函数。最后,还展示了如何使用`vector`创建字符串数组,体现了`vector`在实际编程中的灵活性和实用性。
136 4
|
3月前
|
C语言 C++ 容器
【c++丨STL】string模拟实现(附源码)
本文详细介绍了如何模拟实现C++ STL中的`string`类,包括其构造函数、拷贝构造、赋值重载、析构函数等基本功能,以及字符串的插入、删除、查找、比较等操作。文章还展示了如何实现输入输出流操作符,使自定义的`string`类能够方便地与`cin`和`cout`配合使用。通过这些实现,读者不仅能加深对`string`类的理解,还能提升对C++编程技巧的掌握。
140 5
|
3月前
|
存储 编译器 C语言
【c++丨STL】string类的使用
本文介绍了C++中`string`类的基本概念及其主要接口。`string`类在C++标准库中扮演着重要角色,它提供了比C语言中字符串处理函数更丰富、安全和便捷的功能。文章详细讲解了`string`类的构造函数、赋值运算符、容量管理接口、元素访问及遍历方法、字符串修改操作、字符串运算接口、常量成员和非成员函数等内容。通过实例演示了如何使用这些接口进行字符串的创建、修改、查找和比较等操作,帮助读者更好地理解和掌握`string`类的应用。
89 2
|
2月前
|
存储 编译器 C语言
【c++丨STL】vector模拟实现
本文深入探讨了 `vector` 的底层实现原理,并尝试模拟实现其结构及常用接口。首先介绍了 `vector` 的底层是动态顺序表,使用三个迭代器(指针)来维护数组,分别为 `start`、`finish` 和 `end_of_storage`。接着详细讲解了如何实现 `vector` 的各种构造函数、析构函数、容量接口、迭代器接口、插入和删除操作等。最后提供了完整的模拟实现代码,帮助读者更好地理解和掌握 `vector` 的实现细节。
67 0
|
5月前
|
Java 索引
java基础(13)String类
本文介绍了Java中String类的多种操作方法,包括字符串拼接、获取长度、去除空格、替换、截取、分割、比较和查找字符等。
59 0
java基础(13)String类
|
4月前
|
Java
【编程基础知识】(讲解+示例实战)方法参数的传递机制(值传递及地址传递)以及String类的对象的不可变性
本文深入探讨了Java中方法参数的传递机制,包括值传递和引用传递的区别,以及String类对象的不可变性。通过详细讲解和示例代码,帮助读者理解参数传递的内部原理,并掌握在实际编程中正确处理参数传递的方法。关键词:Java, 方法参数传递, 值传递, 引用传递, String不可变性。
92 1
【编程基础知识】(讲解+示例实战)方法参数的传递机制(值传递及地址传递)以及String类的对象的不可变性