String 初步解析

一.构造函数 

我们在Reference - C++ Reference (cplusplus.com)网站搜索String再查看其构造函数,可以看到下面图片:

这里可以看到C++有关String的构造函数共有七个,写的较为繁琐,我们一 一讲解:

1. string(),默认构造函数,生成的是什么都没有的空串,要注意的是在string所有构造函数中末尾都会自动加一个‘\0’,作为结束的标志,但加入的‘\0’不会显示出来,也不会计入在string 字符个数的数目中。代码测试如下:

#include<string>
using namespace std;

int main()
{

	string s1;
	cout << s1 << endl;
	cout << s1.size() << endl;
	if (s1[0] == '\0')
	{
		cout << "yes" << endl;
	}
}

运行结果:

再看监视窗口:

 可以看出‘\0’并没有计入size,也没有显示出来,但末尾确实加了‘\0’。

2.其他构造函数:先看代码

int main()
{
	string s2("hello world");
	string s3 = s2;//拷贝构造
	string s4(s2);
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
    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);
	cout << s8 << endl;
	string s9(10, 'x');
	cout << s9 << endl;

}

结果如下: 

从中我们可以了解各个构造函数的功能,需要指出的是第三个构造函数,String (const String &str,size_t pos,size_t len=npos),指的是从传入 const String &str的第pos个位置,去len个长度的字符进行初始化,如果len没有传入值,默认为 npos,npos是String中定义的具有size_t类型的最大值的静态变量,如果大于传入的str的长度,到str的末尾自动停止。

二.遍历

这里介绍三种:

int main()
{
  string s1 = "hello world";
	//下标遍历
	for (int i = 0; i < s1.size(); i++)
	{
		cout << s1[i];
	}
	cout << endl;


	//iterator遍历 STL通用
	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		cout << *it;
		++it;
	}
	cout << endl;


    //范围for (其实在编译阶段就会转化为迭代器)
    for (auto e : s1)
	{
		cout << e ;
	}
	cout << endl;



}

注意的是iterator是所有STL都可以使用的,像list,vector都可以用,下标访问并不通用,范围for则是在编译阶段就会转换为迭代器的形式。 

三.string类对象的容量操作

我们重点讲解最后俩个,reserve()是为字符串预留空间,一般只能大不能小,resize()是将有效字符个数改为n个,多出的空间用传入的字符填充,没有传入默认初始化‘\0’。看代码演示

int main()
{
    string s1 = "hello world";
	s1.reserve(100);
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity ()<< endl;
	s1.resize(15, 'a');
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;
	s1.resize(20);
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;
}

 运行结果:

和最后s1的字符串结果

可以验证以上结论,需要注意的的是在vs2022下reserve(n)会预留比n大或和n一样大的空间,并不精准是n的大小,看不同编译器的实现吧。

四.. string类对象的增删查改操作:

 我们先学字符串的增加操作有push_back(),+=,append(),insert():

int main()
{
	string s1("hello");
	s1.push_back(' ');
	s1.append("world");
	cout << s1 << endl;

	string s2 = "xxxxx";//先调用构造函数隐式转换生成临时变量,再进行拷贝构造,但编译器进行了优化
	const string& s3 = "xxxxx";//临时变量具有常性

	s2.append(s1.begin(), s1.end());//append 末尾的是不会加上去的
	const char* p = "hello";
	s2.append(p, p + 6);
	cout << s2 << endl;
	
	s1 += '!';
	
	cout << s1 << endl;
	cout<<endl;
	string s5("hello world");
	s2.insert(5, "xxxxx");//下标从零开始计算
	cout << s5 << endl;
	
	s5.insert(0, 5, 'x');//从第0位置开始插入5个'x'
	s5.insert(s5.begin(),'1');
	cout << s5<< endl;
	return 0;
	
}

运行结果:

其中append()的用法共有以下几种:

 最常用的还是演示的那俩种。

我们再看删除操作,有erase()

看代码:

int main()
{
	string s1("hello world");
	s1.erase(5,4);//从第5个位置开始删除四个字符,包括第5个
	cout << s1 << endl;
	s1.erase(5);//第二个参数默认为npos
	cout << s1 << endl;

	return 0;
}

结果:

最后我们再用一个例子讲述查找find()和修改replace()

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

	//所有空格替换为##
	size_t pos = s1.find(' ',5);//从5个位置开始找' '
	cout << pos << endl;
	while (pos !=string:: npos)
	{
		
		s1.replace(pos,1,"##");//从pos开始用1个字符进行替换,由于插入时后面的要移动因此效率低
		pos = s1.find(' ', pos + 2);
	}
}

 结果:

五.其他常用相关函数

1.c_str()函数返回一个指向正规C字符串的指针常量(const char*), 内容与本string串相同,用途

int main()
{
    char c[20];
	string s = "1234";
	strcpy(c, s.c_str());
	cout << c << endl;
}

此外,在文件中也会用到 

int main()
{
	string filename("Text.cpp");
	FILE* fout = fopen(filename.c_str(), "r");//fopen不接受sting类型,只能调用.c_str()函数
	char ch = fgetc(fout);
	while (ch != EOF)
	{
		cout << ch;
		ch = fgetc(fout);
	}
	return 0;
}

2.getline

我们解释常见的上述俩种:

is :表示一个输入流,例如 cin。
str :string类型的引用,用来存储输入流中的流信息。
delim :char类型的变量,所设置的截断字符(该字符不会计入);在不自定义设置的情况下,遇到’\n’,则终止输入 

看代码:

int main()
{
    string s1;
	cout << "请输入字符串" << endl;
	getline(cin, s1);
	cout << "结果如下:" << endl;
	cout << s1 << endl;
	string s2;
	cout << "请输入字符串" << endl;
	getline(cin, s2, 'a');
	cout << "结果如下:" << endl;
	cout << s2 << endl;
}

 运行结果:

三.substr

int main()
{
    string s2("hello world");
	string s3;
	s3 = s2.substr(1, 2);
	cout << s3 << endl;
}

 运行结果:

作用是从s2第一个位置开始pos1开始截n个字符做子串,第二个参数默认为npos。

String的初步解析就到这,有问题可以评论区提出。 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值