C++初级-string类常用接口

C++初级-string类常用接口

1.string类对象的常见构造

  • string():构造空的string类对象,即空字符串
  • string(const char*s):用C-string来构造string类对象
  • string(size_t n,char c):string类对象中包含n个字符c
  • string(const string&s):拷贝构造函数
#include<cstring>
#include<iostream>

using namespace std;

int main()
{
	string s1;//空字符串
	string s2("hello");//构造string类对象(hello)
	string s3(5, 'a');//string类对象中包含5个a(aaaaa)
	string s4(s2);//拷贝构造(hello)
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	return 0;
}

2.string类对象的容量操作

  • size():返回字符串的有效字符长度
  • length():返回字符串的有效字符长度
  • capacity():返回的空间总大小
  • empty():检测字符串是否为空串
  • clear():清空有效字符
  • reserve(size_t n):为字符串预留空间
  • resize(size_t n,char c):将有效字符个数改成n个,多出的空间用字符c填充
  • size()与length()实现原理相同,引入size()是为了与其他容器接口保持一致
  • clear()只是将字符串中的有效字符清空,不改变底层空间大小
  • resize(size_t n)与resize(size_t n,char c)都是将有效字符个数改变为n
  • reserve(size_t n)为字符串预留空间,不改变有效元素个数
#include<cstring>
#include<iostream>

using namespace std;

void test_1()
{
	string s("hello");
	cout << s << endl;//hello
	cout << s.size() << endl;//5
	cout << s.length() << endl;//5
	cout << s.capacity() << endl;//15

	s.clear();
	cout << s << endl;//空
	cout << s.size() << endl;//0(返回字符串的有效字符长度)
	cout << s.length() << endl;//0(返回字符串的有效字符长度)
	cout << s.capacity() << endl;//15(返回的总空间大小)

	s.resize(5, 'a');//将有效字符个数改成5个,多余的用字符a填充
	cout << s << endl;//aaaaa
	cout << s.size() << endl;//5
	cout << s.length() << endl;//5
	cout << s.capacity() << endl;//15

	s.resize(3);//将有效字符个数改成3个,多余的用字符a填充
	cout << s << endl;//aaa
	cout << s.size() << endl;//3
	cout << s.length() << endl;//3
	cout << s.capacity() << endl;//15
}

void test_2()
{
	string s;
	s.reserve(56);//为字符串预留空间
	cout << s.size() << endl;//0
	cout << s.capacity() << endl;//56
}

int main()
{
	test_1();
	cout << endl;
	test_2();
	cout << endl;
	return 0;
}

3.string类对象的访问及遍历操作

  • operator[]:返回pos位置字符,const string类对象调用

  • begin()+end():begin获取一个字符的迭代器,end获取最后一个字符下一个位置的迭代器

  • rbegin()+rend():begin获取一个字符的迭代器,end获取最后一个字符下一个位置的迭代器

  • 范围for:c++11
    4.string类对象的修改操作

  • push_back(char c):字符串后尾插字符C

  • append(const string s):字符串后追加字符串

  • operator+=:在字符串后追加字符串

  • c_str():返回C格式字符串

  • find()+npos:从字符串pos位置开始往后找字符C,返回该字符在字符串中的位置

  • rfind():从字符串pos位置开始往前找字符C,返回该字符在字符串中的位置

  • substr(size_t pos,size_t n):在字符串中从pos位置开始,截取n个字符返回

#include<cstring>
#include<iostream>

using namespace std;

int main()
{
	string str;
	str.append("hello");//在字符串后追加hello
	str.push_back('a');//在字符串后插入a
	str += "linux";// 在字符串后追加linux
	cout << str << endl;
	cout << str.c_str() << endl;//返回C格式字符串
	return 0;
}

5.string类非成员函数

  • operator+:尽量少用,传值返回,深拷贝效率低
  • operator>>:输入运算符重载
  • operator<<:输出运算符重载
  • getline:获取一行字符串
  • relational operators:大小比较
    (1)翻转字符串
#include<iostream>
#include<cstring>

using namespace std;

string reverseString(string s)
{
	if (s.empty())
	{
		return s;
	}
	size_t start = 0;
	size_t end = s.size() - 1;
	while (start < end)
	{
		swap(s[start], s[end]);
		++start;
		--end;
	}
	return s;
}


int main()
{
	string s("world");
	string str = reverseString(s);
	cout << s << endl;
	cout << str << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值