STL-String的各类接口及用例

1.构造函数

1.1函数接口
函数含义
string()构造空的string类对象
string(const char* s)用c_string来构造string类对象
string(size_t n,char c)string类对象中包含n个字符c
string(const string& s)拷贝构造函数
string(const string& s,size_t n)用s中的前n个字符构造新的string类对象
1.2函数接口用例
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

void TestString()
{
	//string();
	string s1;
	//string(const char* c)
	string s2("hello");
	//string(size_t n,char c)
	string s3(5, 'b');
	//string(const string& s)
	string s4(s3);
	//string(const string& s,size_t n)
	string s5(s4, 5);

	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	cout << s5 << endl;
}
int main()
{
	TestString();
	return 0;
}

2.容量操作函数

2.1 函数接口
函数含义
size_t size() const返回字符串有效字符长度
size_t length() const返回字符串有效字符长度
void clear清空有效字符
size_t capacity ( ) const返回空间总大小
bool empty ( ) const检测字符串释放为空串,是返回true,否则返回false
void resize ( size_t n, char c )将有效字符的个数该成n个,多出的空间用字符c填充
void resize ( size_t n )将有效字符的个数改成n个,多出的空间用0填充
void reserve ( size_t res_arg=0 )为字符串预留空间
2.2函数接口用例
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

void TestString()
{
	string s1("hello");
	cout << s1.size() << endl;
	cout << s1.length() << endl;//5

	cout << s1.capacity() << endl;
	cout << s1.empty() << endl;
	s1.clear();
	cout << s1 << endl;
	cout << s1.capacity() << endl;
	string s2(5, 'a');
	cout << s2.capacity() << endl;
	s2.resize(10, 'b');
	cout << s2 << endl;
	cout << s2.capacity() << endl;
	//void resize(size_t n) 将有效字符个数改为n个,多出的空间用0填充
	s2.resize(3);
	cout << s2 << endl;
	cout << s2.capacity() << endl;
	//resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变

}

void TestString2()
{
	string s;
	//1.测试reserve是否会改变string中有效元素个数
	s.reserve(100);
	cout << s.size() << endl;//输出0,说明reserve不会改变string中有效元素的个数
	cout << s.capacity() << endl;

	// 2.测试reserve参数小于string的底层空间大小时,是否会将空间缩小
	s.reserve(50);
	cout << s.size() << endl;
	cout << s.capacity() << endl;//reserve参数小于string的底层空间大小时,空间不会缩小
}
int main()
{
	TestString2();
	return 0;
}

3.修改函数

3.1 函数接口
函数含义
void push_back(char c)在字符串后尾插字符c
string& append (const char* s)在字符串后追加一个字符串
string& operator+=(const string& str)在字符串后追加字符串str
string& operator+=(const char* s)在字符串后追加C个数字符串
string& operator+=(char c)在字符串后追加字符c
const char* c_str( ) const返回C格式字符串
size_t find (char c, size_t pos =0) const从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置
size_t rfind(char c, size_t pos = npos)从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置
string substr(size_t pos = 0, size_t n= npos)const在str中从pos位置开始,截取n个字符,然后将其返回
3.2函数接口用例
#include <iostream>
#include <string>


using namespace std;
//测试string类对象的修改操作

void TestString()
{
	string str;
	str.push_back(' '); // 在str后插入空格
	str.append("hello"); // 在str后追加一个字符"hello"
	str += 'w'; // 在str后追加一个字符'w'
	str += "orld"; // 在str后追加一个字符串"orld"
	cout << str << endl;
	cout << str.c_str() << endl; // 以C语言的方式打印字符串

	//获取file的后缀

	string file1("test.cpp");
	string file2("test.c.zip");

	size_t pos = file1.rfind(".");
	if (pos != string::npos)//npos是string里的一个静态成员变量
	{
		string su(file1.substr(pos, file1.size() - 1));
		cout << su << endl;
	}

	pos = file2.rfind(".");
	if (pos != string::npos)//npos是string里的一个静态成员变量
	{
		string su(file2.substr(pos, file2.size() - 1));
		cout << su << endl;
	}

}


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

4.string类对象的访问操作函数

4.1函数接口
函数含义
char& operator[] ( size_t pos )返回pos位置的字符,const string类对象调用
const char& operator[] ( size_t pos ) const返回pos位置的字符,非const string类对象调用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值