C++回顾(十九)—— 容器string

19.1 string概述

  • 1、string是STL的字符串类型,通常用来表示字符串。而在使用string之前,字符串通常是 用char * 表示的。string 与char * 都可以用来表示字符串,那么二者有什么区别呢。

  • 2、string和 char * 的比较
    (1)string是一个类, char*是一个指向字符的指针。
    (2)string封装了char * ,管理这个字符串,是一个char * 型的容器。
    (3)string不用考虑内存释放和越界。
    (4)string管理char * 所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。

  • 3、string提供了一系列的字符串操作函数(这个等下会详讲)
    查找find,拷贝copy,删除erase,替换replace,插入insert

19.2 string构造

(1)默认构造函数:

string();    //构造一个空的字符串string s1。

(2)拷贝构造函数:

string(const string &str);  //构造一个与str一样的string。如string s1(s2)。

(3)带参数的构造函数

string(const char *s);    //  用字符串s初始化
string(int n,char c);    //  用n个字符c初始化

例子:
在这里插入图片描述
完整示例代码:

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string s1;                 //无参构造函数
	string s2("helloworld");   //有参构造函数
	string s3(10, 'a');   
	string s4(s2);            //拷贝构造函数

	cout << s1 << endl;   //重载了输出运算符 << 
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;

	cin >> s1;   //重载了输入运算符 >> 
	cout << s1 << endl;

	s1 += "helloworld";  // 重载了 += 运算符
	if (s1 == s2)	// 重载了 == 运算符
	{
	}

	s1 = s1 + s2;   // 重载了 = 运算符

	return 0;
}

运行结果:
在这里插入图片描述

19.3 string使用

(1)string的存取字符操作:[ ] 或者 s.at( )

  • string类的字符操作:
const char &operator[] (int n) const; // 常函数(不改变成员变量)
const char &at(int n) const;  // 常函数(不改变成员变量)
char &operator[] (int n);
char &at(int n);
  • operator[]和at()均返回当前字符串中第n个字符,但二者是有区别的。
    主要区别在于at()在越界时会抛出异常,[ ]在刚好越界时会返回(char)0,再继续越界时,编译器直接出错。如果你的程序希望可以通过try,catch捕获异常,建议采用at()。
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    完整示例代码:
#include <iostream>
#include <exception>

using namespace std;

int main()
{
	string s("helloworld");

	cout << s[1] << endl;        //重载了下标运算符
	s[1] = 'x';

	cout << s << endl;

	cout << s.at(1) << endl;     //通过成员函数来访问下标为 1 的元素

	//cout << s[20000] << endl;    //越界访问程序异常结束
	try{
		cout << s.at(10) << endl;      //使用成员函数at(),越界访问会抛出异常
	}
	catch (exception &e)
	{
		cout << e.what() << endl;
	}

	return 0;
}

运行结果:
在这里插入图片描述

(2)从string取得字符串首地址的操作:s.c_str( )

  • const char *c_str() const; //返回一个以’\0’结尾的字符串的首地址
    在这里插入图片描述
    完整示例代码:
#include <iostream>
#include <string.h>
#include <string>

using namespace std;

int main()
{
	char buf[32] = {0};
	string s("helloworld");

	strcpy(buf, s.c_str());     //c_str()返回字符串的首地址
	
	cout << buf << endl;

	return 0;
}

运行结果:
在这里插入图片描述

(3)把string拷贝的操作:s.copy( )

把字符串s从第0给下标开始,拷贝5个字符到buf中去。
在这里插入图片描述在这里插入图片描述
完整示例代码:

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
	char buf[32] = {0};

	string s("helloworld");
	s.copy(buf, 5);               //第二个参数表示拷贝5个字节,第三个参数是默认参数,拷贝的位置,默认是0
	cout << buf << endl;

	memset(buf, 0, sizeof(buf));
	s.copy(buf, 4, 5); // 拷贝四个字节,从下标为5的字符开始拷贝
	cout << buf << endl;

	return 0;
}

运行结果:
在这里插入图片描述

(4)string的长度:s.length() 与 s.empty()

完整示例代码:

#include <iostream>

using namespace std;

int main()
{
	string s("helloworld");

	cout << s.length() << endl;

	if (s.empty())
	{
		cout << "长度为空" << endl;
	}
	else
	{
		cout << "长度不为空" << endl;
	}
	return 0;
}

运行结果:
在这里插入图片描述

(5)string的赋值:重载的 = 运算符 与 s.assign()

示例代码:

#include <iostream>

using namespace std;

int main()
{
	string s1("helloworld");

	s1 = "hello";        //重载了=运算符
	cout << s1 << endl;

	const char *s = "this is test";
	s1.assign(s);  // 把s这个字符串赋值给s1
	cout << s1 << endl;

	s1.assign(s, 7); //  把s这个字符串的前7个字符赋值给s1
	cout << s1 << endl;

	s1.assign(5, 'a');     //把5个a赋值给s1
	cout << s1 << endl;

	string s2("hey boy");
	s1.assign(s2);         //把对象s2赋值给s1
	cout << s1 << endl;

	s1.assign(s2, 4, 3); // 把s2从第四个字符开始往后3个字符,赋值给字符串s1
	cout << s1 << endl;

	return 0;
}

运行结果:

在这里插入图片描述

(6)string字符串连接:重载 += 运算符 与 s.append()

示例代码:

#include <iostream>

using namespace std;

int main()
{
	string s1("helloworld");

	s1 += "1234";    //重载了 += 运算符
	cout << s1 << endl;

	string s2("abcdefg");
	s1 += s2;
	cout << s1 << endl;

	const char *s = "haha";
	s1.append(s);
	cout << s1 << endl;

	s1.append(s, 2); // 把 s 前两个字符加在s1后面
	cout << s1 << endl;

	s1.append(s2);
	cout << s1 << endl;

	s1.append(s2, 4, 2);  // 把 s 第四个字符开始往后两个字符加在s1后面
	cout << s1 << endl;

	s1.append(10, 'x'); // 把10个x字符追加在字符串
	cout << s1 << endl;

	return 0;
}

运行结果:
在这里插入图片描述

(7)string的比较:s.compare()

示例代码:

#include <iostream>

using namespace std;

int main()
{
	string s1("helloworld");
	string s2("helloboy");
	const char *s = "hellogirl";

	if (s1.compare(s2) > 0)
	{
		cout << s1 << " > " << s2 << endl;
	}

	if (s1.compare(s) > 0)
	{
		cout << s1 << " > " << s << endl;
	}
	return 0;
}

运行结果:
在这里插入图片描述

(8)string的子串:s.substr()

示例代码:

#include <iostream>

using namespace std;

int main()
{
	string s("helloworld");

	cout << s.substr() << endl; // 返回完整的字符串s
	cout << s.substr(5, 5) << endl; // 从下标五开始,往后五个字符

	return 0;
}

运行结果:

在这里插入图片描述

(9)string的查找和替换:s.find() 与 s.replace()

  1. 查找
    int find(char c,int pos=0) const; //从pos开始查找字符c在当前字符串的位置

    int find(const char *s, int pos=0) const; //从pos开始查找字符串s在当前字符串的位置

    int find(const string &s, int pos=0) const; //从pos开始查找字符串s在当前字符串中的位置

    注意:find函数如果查找不到,就返回-1

    int rfind(char c, int pos=npos) const; //从pos开始从后向前查找字符c在当前字符串中的位置
    int rfind(const char *s, int pos=npos) const; int rfind(const string &s, int pos=npos) const;
    //rfind是反向查找的意思,如果查找不到, 返回-1

  2. 替换
    string &replace(int pos, int n, const char *s);//删除从pos开始的n个字符,然后在pos处插入串s
    string &replace(int pos, int n, const string &s); //删除从pos开始的n个字符,然后在pos处插入串s
    void swap(string &s2); //交换当前字符串与s2的值

完整示例代码:

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
	int p;
	string s1("helloworld");
	string s2("world");

	p = s1.find('o');
	cout << p << endl; // 4

	p = s1.find('x');    //不存在返回-1
	cout << p << endl; // -1

	p = s1.find('o', 5);
	cout << p << endl; // 6

	p = s1.find("ll");
	cout << p << endl; // 2

	p = s1.find(s2);
	cout << p << endl; // 5

	p = s1.rfind('o');    //反向查找(但是返回的位置还行从左边0开始)
	cout << p << endl; // 6

	s1.replace(5, 5, "xxx");
	cout << s1 << endl; // helloxxxxx

	s1.replace(5, 3, s2);
	cout << s1 << endl; // helloworld

	string s3("helloworldhelloworldhelloworldhelloworld");
	p = s3.find("world");
	while (p != -1)
	{
		s3.replace(p, strlen("world"), "x");
		p = s3.find("world", p + strlen("x"));
	}
	cout << s3 << endl;

	return 0;
}

运行结果:
在这里插入图片描述

(10)String的区间删除和插入:s.insert() 与 s.erase()

完整示例代码:

#include <iostream>

using namespace std;

int main()
{
	string s1("helloworld");
	string s2("12345");

	s1.insert(0, "this is ");// 从下标为0处,插入this is
	cout << s1 << endl; 

	s1.insert(10, s2); // 从下标为10处,插入s2
	cout << s1 << endl; 

	s1.insert(0, 5, 'x'); // 从下标为0处,插入5个x
	cout << s1 << endl;

	s1.erase(0, 20);
	cout << s1 << endl; // 删除下标0到20

	return 0;
}

运行结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值