STL--string容器

目录

一、string容器的基本概念

二、string的构造函数

 三、string赋值操作

 四、string字符串拼接

 五、string字符串查找和替换

 六、string字符串比较

 七、string字符串中的字符存取

 八、string字符串的插入和删除

 九、string字符串的子串


一、string容器的基本概念

1. 概念:

        C++string 是C++中的字符串。 字符串对象是一种特殊类型的容器,专门设计来操作的字符序列。 不像传统的c-strings,只是在数组中的一个字符序列,我们称之为字符数组,而C + +字符串对象属于一个类,这个类有很多内置的特点,在操作方式,更直观,另外还有很多有用的成员函数。 string 的定义为:typedef basic_string string。

2. 本质:string是C++风格的字符串,而string本质上是一个类。

3. string和char*的区别:

  1. char*是一个指针
  2. string是一个类,内部封装了char*,管理这个字符串,是一个char*型的容器

4. 特点:

  1. string 类内部封装了很多成员方法

  2. 例如,查找find,拷贝copy,删除delete,替换 replace,插入 insert

  3. string 管理char* 所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责

二、string的构造函数

  • string();                          // 创建一个空的字符串
  • string(const char* s);            // 使用字符串初始化
  • string(const string & str);   // 使用一个string对象初始化另一个string对象
  • string(int n,char c);               // 使用n个字符c初始化

示例 :

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s1;// 无参构造,默认构造

	const char* str = "hello world";
	string s2(str);
	cout << "s2=" << s2 << endl;

	string s3(s2);
	cout << "s3是拷贝构造:" << s3 << endl;

	// 初始化n个字符
	string s4(10, 'a');
	cout << "初始化n个字符:" << s4 << endl;
	return 0;
}

运行结果:

 

 三、string赋值操作

  • string & operator=(const  char* s);                //  char* 类型字符串赋值给当前的字符串
  • string & operator=(const  string &s);          // 把字符串s赋值给当前的字符串
  • string & operator=(char c);                       // 把字符赋值给当前字符串
  • string & assign(const char* s);                       //  把字符串s赋值给当前字符串
  • string & assign(const  char* s,int n);           // 把字符串前n个字符赋给当前的字符串
  • string & assign(const string &s);                 // 把字符串s赋给当前的字符串
  • string & assign(int n,char c);                        // 用n个字符c给当前字符串

示例: 

#include<iostream>
#include<string>
using namespace std;
int main()
{
	// 直接=一个字符串
	string str1;
	str1 = "hello world";
	cout << "直接=一个字符串\t str1=" << str1 << endl;

	// =另一个string类型的字符串
	string str2;
	str2 = str1;
	cout << "=另一个string类型的字符串\t str2=" << str2 << endl;

	// 赋值单个字符
	string str3;
	str3 = 'a';// 赋值一个字符
	cout << "赋值单个字符\t str3=" << str3 << endl;

	// 利用assign的内置函数赋值
	string str4;
	str4.assign("hello C++ assign");
	cout << "利用assign的内置函数赋值\t str4=" << str4 << endl;

	// 利用assign赋值字符串的前n个字符
	string str5;
	str5.assign("hello C++", 5);// 前五个字符
	cout << "利用assign赋值字符串的前n个字符\t str5=" << str5 << endl;

	// 拷贝另一个string类型的字符串
	string str6;
	str6.assign(str5);
	cout << "拷贝另一个string类型的字符串\t str6=" << str6 << endl;

	// n个字符
	string str7;
	str7.assign(10, 'w');
	cout << "n个字符\t str7=" << str7 << endl;
	return 0;
}

运行结果:

 

 四、string字符串拼接

  • string & operator+=(const char* str);                            //  重载+= 操作符
  • string & operator+=(const char c);                                // 重载+=操作符
  • string & operator+=(const string & str);                      // 重载+=操作符
  • string & append(const char *s);                                  // 把字符串s连接到当前字符串结尾
  • string & append(const char * s , int  n);                        // 把字符串前n个字符连接到当前字符串结尾
  • string & append(const string & s);                                // 与第一个一样可以直接把字符串拼接到字符串结尾
  • string & append(const string & s ,int  pos , int  n);        // 字符串s从pos开始的n个字符连接到字符串结尾

示例: 

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1 = "hello  你好。";
	cout << "最初的字符串:" << str1 << endl;

	// 追加一个字符串
	str1 += "这是一个char*类型的字符串";
	cout << "利用\"+=\"拼接后:" << str1 << endl;

	// 追加一个字符
	str1 += ':';
	cout << "利用\"+=\"追加一个字符:" << str1 << endl;

	// 追加一个string类型的字符串
	string str2 = "这是一个string类型的字符串";
	str1 += str2;
	cout << "利用\"+=\"追加一个string类型的字符串" << str1 << endl;

	// append()方式追加
	string str3 = "i";
	cout << endl << "append形式追加的原始字符串:" << endl;
	cout << "str3=" << str3 << endl;
	str3.append(" love");
	cout << "利用\"append\"的方式追加:" << str3 << endl;

	// 追加字符串的前n个字符
	str3.append(" You  YOU", 4);
	cout << "利用\"append\"追加前n个字符:" << str3 << endl;

	// 追加一个字符串
	str3.append(" 某某某");
	cout << "利用\"append\"追加一个字符串:" << str3 << endl;

	// 从pos开始的位置截取一个字符串
	str3.append(str3, 1, 5);// 从pos位置开始截取n个字符
	cout << "利用\"append\"截取1-5位置的字符串:" << str3 << endl;
	return 0;
}

运行结果:

 

 五、string字符串查找和替换

查找:查找指定位置的字符串是否存在

替换:在指定的位置替换字符串

  • int find(const string & str , int pos = 0) const;                   // 查找str第一次出现的位置,从pos开始查找
  • int find(const  char* s, int  pos = 0)  const;                        // 查找s第一次出现的位置,从pos开始查找
  • int find(const  char* s , int  pos , int  n) const;                    // 从pos位置查找s的前n个字符第一次出现的位置
  • int find(const  char c ,int pos = 0)  const;                           // 查找字符c第一次出现的位置
  • int rfind(const  string & str , int pos = npos )const;          // 查找字符串str最后一次出现的位置,从pos开始查找
  • int rfind(const  char* s, int pos = npos) const;                  // 查找s最后一次出现的位置,从pos开始查找
  • int rfind(const  char* s ,int pos , int n) const;                     // 从pos查找s的前n个字符的最后一次位置
  • int rfind(const  char c, int pos = 0) const;                          // 查找字符c最后一次出现的位置
  • string & replace(int pos ,int n,const string & str);              // 替换从pos开始n个字符为str
  • string & replace(int pos,int n,const char* s);                      // 替换从pos开始的n个字符为字符串s

示例: 

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1 = "hello world";
	cout << "原本的字符串" << str1 << "\n要找的字符串" << "\"ello\"" << endl;
	int pos = str1.find("ello");// 返回的是第一个字符的位置,从零开始
	cout << "第一个字符出现的位置:" << pos << endl;
	cout << "要找的第二个字符串" << "\"mmm\"" << endl;
	pos = str1.find("mmmm");
	cout << "没有找到返回(-1):" << pos << endl;

	// 从后开始找rfind
	// 适用于一个字符串有多个字符时的使用
	// find是左边第一个出现的位置,rfind是右边第一次出现的位置
	cout << "find是左边第一个出现的位置,rfind是右边第一次出现的位置" << endl;
	cout << "\"rfind\"找" << "\"ello\":" << endl;
	pos = str1.rfind("ell");
	cout << "从后向前找返回的位置:" << pos << endl;// 也是第一个字符出现的位置,从零开始

	// 替换
	string str2 = "abcdefghijklmnopqrstuvwxyz";
	// 如果替换的字符串少于原本的个数,也会把原本的字符串替换掉
	// 如果替换的字符串多于原本的字符串个数,也会把所有的字符串插入,其余字符串后移
	cout << "原本的字符串:" << str2 << endl;
	str2.replace(1, 3, "11");
	cout << "替换完成后;" << str2 << endl;
	str2.replace(4, 3, "2222");
	cout << "多于的情况:" << str2 << endl;
	return 0;
}

运行结果:

 六、string字符串比较

按照ACSCII码的方式比较

比较方式:

=   返回   0

>返回   1

< 返回  -1

函数原型:

  • int  compare(const string & s)  const;      // 与字符串s比较
  • int  compare(const  char* s  )const;       // 与字符串s比较

示例: 

#include<iostream>
#include<string>
using namespace std;
int main()
{
	//字符串的比较
	string str1="ad";
	string str2="bc";
	// >  返回 1
	// <  返回 -1
	// =  返回 0
	cout<<str1.compare(str2)<<endl;
	return 0;
}

运行结果:

 

 七、string字符串中的字符存取

中文不可以

  • char & operator[](int  n);    // 通过[]方式取字符
  • char & at(int  n);                 // 通过at方法获取字符

示例: 

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1 = "hello world";
	cout << "原始字符串:" << str1 << endl;
	cout << "利用[]的形式遍历" << endl;
	for (int i = 0; i < str1.size(); i++)
	{
		cout << str1[i] << " ";
	}
	cout << endl;
	cout << "利用\"at()\"的形式遍历" << endl;
	for (int i = 0; i < str1.size(); i++)
	{
		cout << str1.at(i) << " ";
	}
	cout << endl;
	return 0;
}

运行结果:

 

 八、string字符串的插入和删除

只能进行原字符串大小内的插入,不能尾插

  • string & insert(int pos, const char* s);               // 插入字符串
  • string & insert(int pos, const string & s);          // 插入字符串
  • string & insert(int pos, int n, char c);                // 在指定位置插入n个字符c
  • string & erase(int pos, int  n = nops);              // 删除从pos开始的n个字符

示例: 

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1 = "hello world";
	cout << "原始字符串:" << str1 << endl;
	cout << "插入字符串:" << endl;
	str1.insert(0, "这是\"char*\"类型的");
	cout << str1 << endl;

	string str2 = "这是\"string\"类型的";
	str1.insert(0, str2);
	cout << str1 << endl;

	// 插入n个字符
	cout << "指定位置开始插入n个字符" << endl;
	str1.insert(2, 5, '1');
	cout << str1 << endl;

	// 删除
	cout << "删除从pos开始的n个字符" << endl;
	str1.erase(2, 5);
	cout << str1 << endl;
	return 0;
}

运行结果:

 

 九、string字符串的子串

string substr(int pos = 0, int n = npos) const;         // 返回由pos开始的n个字符组成的字符串

示例: 

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1 = "hello world";
	cout << "原始的字符串:" << str1 << endl;
	// 从pos截取n个
	string str2 = str1.substr(1, 4);
	cout << "从位置1-4截取" << endl;
	cout << str2 << endl;

	// string 的应用,find找到位置 , substr截取子串
	string str3 = "20201584796@qq.com";
	cout << "原始的字符串:" << str3 << endl;
	int pos = str3.find('@');
	string str4 = str3.substr(0, pos);
	cout << str4 << endl;
	str4 = str3.substr(pos + 1, str3.size());
	cout << str4 << endl;
	return 0;
}

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值