【C++】string类

目录

1. string类对象的常见构造

2 string类对象的容量操作

3.strin类对象的访问及变量操作

4.string类对象的修改操作

5.string类非成员函数


1. string类对象的常见构造

        string的构造函数含有多种重载,常用如下:

// 1
string();//构造空的string类对象,即空字符串;

// 2
string(const char* s)// 用字符串 s 来构造string类;

// 3
string(size_t n, char c)//用n个字符c来构造string类;

// 4
string(const string& str)//用string类对象str,构造string类。即拷贝构造;

代码实例:

// 1
string str1(); //构造一个空类

// 2
string str2("Hello World!!!!"); //用字符串Hello World!!!!构造一个类

// 3
string str3(5,'H');//用5个字符构造一个类

// 4
string str4(str2);//构造一个和str2一样的类

2 string类对象的容量操作

        1)size : 返回自字符串的有效长度;

函数原型: size_t size ( ) const

        2)legth : 返回自字符串的有效长度;

函数原型: size_t length  ( ) const

        3) capacity : 返回空间总大小(不包含‘\0’);

函数原型: size_t capacity ( ) const

        4) reserve : 为字符串提前开辟空间;

函数原型 : void reserve (size_t n = 0)

         reserve()函数只开空间,也只改变容量。当知道需要多大空间时可以提前开好,以免扩容产生性能不必要的浪费。

代码:

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

void text1()
{

	string str1("hello world!!!");

	size_t size = str1.size(); //字符串的长度
    cout << size << endl;
	cout << endl;

	size_t capacity = str1.capacity(); //总空间的大小
	cout << capacity << endl;

	str1.reserve(100);

	cout << endl;
	size = str1.size(); //字符串的长度
	cout << size << endl;

	cout << endl;
	capacity = str1.capacity(); //总空间的大小
	cout << capacity << endl;

}

int main()
{
	text1();
	return 0;
}

        5) resize : 将有效字符的个数改成n个

函数原型: void resize ( size_t  n)

                   void resize (size_t  n, char c)

         1)当 n 小于 capacity时,只改变 size 的大小,将size改为n,不改变capacity的大小;

执行resize( 5 )前:

 执行后:

 结果:

         2)当 n 大于 capacity时,size、 capacity 的大小都会改变,并且会用 ‘\0’填充,若为函数2则用字符ch 填充;

执行resize( 50 )前:

执行后:

 

 

 代码:

void text1()
{

	string str1("hello world!!!");

	size_t size = str1.size(); //字符串的长度
    cout << size << endl;
	cout << endl;

	size_t capacity = str1.capacity(); //总空间的大小
	cout << capacity << endl;

	str1.resize(50);

	cout << endl;
	size = str1.size(); //字符串的长度
	cout << size << endl;

	cout << endl;
	capacity = str1.capacity(); //总空间的大小
	cout << capacity << endl;

}

结果:

         6)empty : 检测字符串是释放为空串,是返回true,不是返回 false

函数原型:bool empty() const

         7)clear :清空字符串

函数原型:void clear()

将字符串清空,改变size,但是不会改变空间大小

 注意:

        1.size()和length()方法实现底层原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本有size();

        2.clear()只是将string中的有效字符清空,不改变底层空间大小。

        3.resize ( size_t  n) 与 resize (size_t  n, char c)都是将字符串中的有效字符个数增加到n个,不同的是当字符串个数增多时,resize ( size_t  n)用0来填充多出的空间,resize (size_t  n, char c)用字符c。resize在改变元素个数时,如果将元素个数增多,可能会改变底层容量的大小,如果元素减小,底层空间不变。

        4.reserve为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间大小时,reserve不会改变容量的大小。

3.strin类对象的访问及变量操作

        1)operator[] : 返回pos位置的字符

函数原型:char& operator[] (size_t pos)

                  const char& operator[] (size_t pos)const

 因为函数是传引用返回,所以该字符可以直接被修改;即:

 当然了如不想被更改,只能访问,那可以定义为const类型

 代码:

void text2()
{
	const string str1("hello world!!!");

	cout << str1[0] << endl;

	cout << endl;
	cout << str1 << endl;
	//str1[0] = 'H';
	cout << str1 << endl;

}

        2)迭代器 begin  end :begin获取第一个字符迭代器,end获取最后一个字符下一个位置的迭代器。

函数原型:iterator begin();

                  const_iterator begin() const;

                  iterator end();

                  const_iterator end() const;

迭代器像指针,它的底层实现有可能是指针,也有可能不是指针,但是它的使用方法和指针样。

 可以遍历字符串如①所示,也可更改字符串如②所示。

代码:

void text3()
{
	string s1("hello world!!!");
	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		cout << *it;
		++it;
	}

	cout << endl;
	it = s1.begin();
	while (it != s1.end())
	{
		(*it)++;//更改字符
		cout << *it;
		++it;

	}
}

 若使用const类型的迭代器,字符串也将不能改变。

        3)反向迭代器 rbegin ,rend : rbegin获取最后一个字符位置的迭代器,rend获取第一个字符前一个位置的迭代器,与begin和end刚好相反。遍历字符串也是倒着遍历;

 当然它也可更改字符串。

代码:

void text4()
{
	string s1("hello world!!!");
	string::reverse_iterator it = s1.rbegin();
	while (it != s1.rend())
	{
		cout << *it;
		++it;
	}

	cout << endl;
	it = s1.rbegin();
	while (it != s1.rend())
	{
		(*it)++;
		cout << *it;
		++it;

	}
}

 若不想字符串被改变可用const修饰。

        5)范围for 

        除了用迭代器遍历字符串之外,也可用范围for,范围for的底层原理便是迭代器。

范围for 自动迭代,自动判断结束

 代码:

void text5()
{
	string s1("hello world!!!");

	for (auto e : s1)//依次取出s1中字符给e;
	{
		cout << e;
	}
	cout << endl;

}

 当然f范围for也可更改字符,用引用即可:

 代码:

void text6()
{
	string s1("hello world!!!");

	for (auto& e : s1)//依次取出s1中字符给e;
	{
		e++;
		cout << e;
	}
	cout << endl;

}

4.string类对象的修改操作

        1)push_back :在字符串后尾插字符;

函数原型: void push_back(char  c)

在字符串后尾插字符c;

        2)pop_back :  删除最后一个字符;

函数原型: void pop_back( );

        3)append : 在字符串后面追加一个字符串

函数原型:string& append (const char* s)  (不止这一种,其他可参考string类文档)

        4)operator+= :  在字符串后加上字符或者字符串;

函数原型:string& operator+=(const string& str);

                  string& operator+=  (const char* s);

                  string& operator+=  (const char c);

        5)c_str :返回C格式字符串(返回一个指针,这个指针指向由string类的字符所构成的数组)

函数原型: const char* c_str( ) const;

        6)find:从字符串pos位置开始往后找字符/字符串,返回该字符/字符串的位置

函数原型:size_t find (const string& str, size_t pos = 0) const;

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

                   size_t find(char c , size_t pos = 0) const;

从pos的位置往后找,在字符串中找到与之完全相同的子串且是第一次出现,返回下标,若未找到返回 string:npos;  nops 为 static const size_t npos = -1,即:size_t类型的最大值;

        7)rfind: 从字符串pos位置开始往前找字符/字符串,返回该字符/字符串的位置

函数原型:size_t rfind (const string& str, size_t pos = npos) const;

                   size_t rfind (const char* s, size_t pos  =  npos)  const;

                   size_t rfind(char c , size_t pos = pons) const;

 与函数find的功能基本相同 ,区别在于rfind是用pos的位置往前找。

        8)substr : 在字符串中从pos位置开始,截取n个字符,然后将其返回;

函数原型:string substr (size_t pos = 0, size_t len = npos) const;

 从pos的位置截取len长度的字符串,并以string类返回;

 注意:

  •  在string尾部追加字符时,push_back , append ,  +=  三种的实现方式差不多,一般情况下string类的+=操作用的较多,+=不仅能够连接字符,也可连接字符串。
  • 对string操作时,如果能大概预估到放多少字符,可以先通过reserve把空间预留好

5.string类非成员函数

        1)operator<<: 输出运算符重载; operator>> : 输入运算符重载

输出运算符重载是将string类类型直接输出,前面已经用过,不在赘述;

输入运算符重载:即将你在键盘上输入的字符放入string类里,但是它有个缺陷,遇到空格 或者换行符,就会结束本次输入,即当我输入 HHH空格HH是在string类里只有HHH;

 空格后面的两个HH将保存在缓冲器,如果后面依然后cin<<将直接给string类不用再输入

 那问题来了 我想输入带有空格的字符串怎么办? 那就要借助下面一个函数了

2)getline:

函数原型:istream& getline (istream& is, string& str);

该函数可以实现输入空格:还是我要输入HHH空格HH:


以上是string常用的接口,除此之外string类还有很多接口,感兴趣的伙伴可以去查手册。以上就是本篇博客的全部内容,若有错误,欢迎各位佬指正。感激不尽!!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值