string类(二)

String类关键字

1. string类对象的常见构造(string)

功能:构造一个字符对象,根据使用的构造函数版本初始化其值

1.1 空字符串

string s1;

构造一个空字符对象s1.

在这里插入图片描述

1.2 字符串拷贝构造

string s1("hello solity");

可以写成如下方式,也更推荐下面写法:

string s1 = "hello solity";

这种写法属于隐式转换,将const char* 转换成了str类型。

1.3 子字符串

写法:

string (const string& str, size_t pos, size_t len = npos);

意思就是:在str对象的pos位置开始,依次向后取npos个字符。

例子:

#include <iostream>
using namespace std;
#include <string>
int main()
{
	string s1 = "hello solity";
	string pos = string(s1, 6, 3); //从s1的第6个位置开始,向后取3个字符
	cout << pos << endl;
	return 0;
}

在这里插入图片描述

如果取的个数超出了字符串原本的字符个数,则是有多少取多少:

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

int main()
{
	string s1 = "hello solity";
	string pos = string(s1, 6, 9);
	cout << pos << endl;
	return 0;
}

在这里插入图片描述

如果第三个缺省值不给,则表示复制str对象从第pos个位置开始往后的所有字符。

写法:

string (const string& str, size_t pos);

举例:

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

int main()
{
	string s1 = "hello solity";
	string pos = string(s1, 6);
	cout << pos << endl;
	return 0;
}

在这里插入图片描述

如果需要取前n个字符:

写法:

string (const char* s, size_t n);

举例:

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

int main()
{
	string s1("hello solituy",7);  //拷贝前7个字符
	cout << s1 << endl;
	return 0;
}

在这里插入图片描述

2. string类对象的容量操作

2.1 size(str) (重点)

功能:返回字符串str的长度(以字节为单位)

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

int main()
{
	string s1("hello solity");
	int pos1 = size(s1);
	int pos2 = s1.size();  //两种写法
	cout << pos1 << endl;
	cout << pos2 << endl;
	return 0;
}

运行结果:

在这里插入图片描述

2.2 length( )

功能:返回字符串有效字符长度

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

int main()
{
	string s1("hello solity");
	int pos = s1.length();
	cout << pos << endl;
	return 0;
}

运行结果:

在这里插入图片描述

2.3 capacity

功能:返回已分配存储空间的大小

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

int main()
{
	string s1;
	string s2("hello solity");
	string s3("hello solity hello world");
	int pos1 = s1.capacity();
	int pos2 = s2.capacity();
	int pos3 = s3.capacity();
	cout <<"pos1 = " <<  pos1 << endl;
	cout << "pos2 = " << pos2 << endl;
	cout << "pos3 = " << pos3 << endl;
	return 0;
}

此容量不一定等于字符串长度。它可以等于或更大,额外的空间允许对象在向字符串添加新字符时优化其操作。

运行结果:

在这里插入图片描述

从结果可以看出来默认分配的空间大小为15。

2.4 empty (重点)

功能:检测字符串释放为空串,是返回true(1),否则返回false(0)。

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

int main()
{
	string s1;
	string s2("hello solity");
	int pos1 = s1.empty();
	int pos2 = s2.empty();
	cout << "pos1 = " <<  pos1 << endl;
	cout << "pos2 = " << pos2 << endl;
	return 0;
}

运行结果:

在这里插入图片描述

2.5 clear (重点)

功能:擦除字符串的内容,该字符串将成为空字符串(长度为 0个字符)

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

int main()
{
	string s1("hello solity");
	int Before = s1.length();
	cout << "Before = " << Before << endl;

	s1.clear();
	int After = s1.length();
	cout << "After = " << After << endl;
	return 0;
}

运行结果:

在这里插入图片描述

2.6 reserve(重点)

语法: void reserve (size_t n = 0);

功能:请求将字符串容量调整为计划的大小更改,长度最多为 n 个字符

注意:生成的字符串容量可能等于或大于 n !!!

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

int main()
{
	string s1;
	int Before = s1.capacity();
	cout << "Before = " << Before << endl;

	s1.reserve(100);
	int After = s1.capacity();
	cout << "After = " << After << endl;
	return 0;
}

运行结果:

在这里插入图片描述

可以发现,预定分配100个空间,结果系统分配了111个空间。

2.7 resize(重点)

语法:

void resize (size_t n);  //第一种
void resize (size_t n, char c);  //第二种

功能:将字符串的大小调整为 n 个字符的长度。

如果 n 小于当前字符串长度,则当前值将缩短为其第一个 n 个字符,从而删除第 n个字符之外的字符。

如果 n 大于当前字符串长度,则通过在末尾插入任意数量的字符来扩展当前内容,以达到 n 的大小。如果指定了 c,则新元素将初始化为 c 的副本,否则,它们是值初始化的字符(空字符)。

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

int main()
{
	string s1("hello solity");
	s1.resize(8);
	cout << s1 << endl;

	string s2("solity");
	s2.resize(10);
	cout << s2 << endl;
	return 0;
}

调试运行结果:

在这里插入图片描述

可以发现扩大的字符内容,系统用‘\0’代替了!!

3.string类对象的修改操作

3.1 push_back

语法: void push_back (char c);

功能:将字符 c 追加到字符串的末尾,使其长度增加 1

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

int main()
{
	string s1("hello solity");
	s1.push_back('a');
	cout << s1 << endl;
	return 0;
}

运行结果:

在这里插入图片描述

3.2 append

功能:通过在字符串的当前值末尾追加其他字符来扩展字符串

区别:与push_back的不同就在于push_back添加的是一个字符,而append添加的是字符串。

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

int main()
{
	string s1("hello");
	cout << s1 << endl;

	s1.append("solity");
	cout << s1 << endl;
	return 0;
}

运行结果:

在这里插入图片描述

3.3 operator+=(实用)

功能:在字符串后追加字符串

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

int main()
{
	string s1("hello");
	cout << s1 << endl;

	s1 += "so";
	cout << s1 << endl;

	s1 += "lity";
	cout << s1 << endl;
	return 0;
}

运行结果:

在这里插入图片描述

3.4 c_str

功能:获取等效的 C 字符串

c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同

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

int main()
{
    const char* pos;
    string s1 = "hello solity";
    pos = s1.c_str();
    cout << pos << endl;
    s1 = "abcde";
    cout << pos << endl;
	return 0;
}

运行结果:

在这里插入图片描述

3.5 find + npos

语法(find): size_t find (const string& str, size_t pos = 0) const;

功能(find):在字符串中搜索其参数指定的序列的第一次匹配项。

​ 指定 pos 时,搜索仅包括位置 pos 处或位置之后的字符,忽略包含 pos 之前字符的任何可能出现项。

功能(pos):NPOS是一个静态成员常量值,对于类型为 size_t的元素具有最大可能值。

​ 此值在字符串的成员函数中用作 len(或 sublen)参数的值时,表示“直到字符串的末尾”。

3.6 substr

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

功能:在str中从pos位置开始,截取n个字符,然后将其返回

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

int main()
{
    string s1 = "hello solity world";
	string s2 = s1.substr(5, 11);
	cout << s2 << endl;
	return 0;
}

运行结果:

在这里插入图片描述

3.7 getline

功能:获取一行字符串

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

int main()
{
	string name;

	cout << "Please, enter your full name: ";
	getline(cin, name);
	cout << "Hello, " << name << "!\n";
	return 0;
}

运行结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值