C++ -- STL简介、string的使用

什么是STL

 

STL(standard template libaray-标准模板库):是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架。

STL的版本

  • 原始版本:Alexander Stepanov、Meng Lee 在惠普实验室完成的原始版本,本着开源精神,他们声明允许任何人任意运用、拷贝、修改、传播、商业使用这些代码,无需付费。唯一的条件就是也需要向原始版本一样做开源使用。 HP 版本--所有STL实现版本的始祖。
  • P. J. 版本:由P. J. Plauger开发,继承自HP版本,被Windows Visual C++采用,不能公开或修改,缺陷:可读性比较低,符号命名比较怪异。
  • RW版本:由Rouge Wage公司开发,继承自HP版本,被C+ + Builder 采用,不能公开或修改,可读性一般。
  • SGI版本:由Silicon Graphics Computer Systems,Inc公司开发,继承自HP版 本。被GCC(Linux)采用,可移植性好,可公开、修改甚至贩卖,从命名风格和编程 风格上看,阅读性非常高。我们后面学习STL要阅读部分源代码,主要参考的就是这个版本。

STL的六大组件

string类

为什么学习string类

C语言中的字符串

C语言中,字符串是以'\0'结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数,但是这些库函数与字符串是分离开的,不太符合OOP的思想,而且底层空间需要用户自己管理,稍不留神可能还会越界访问。

标准库中的string类

  1. 字符串是表示字符序列的类
  2. 标准的字符串类提供了对此类对象的支持,其接口类似于标准字符容器的接口,但添加了专门用于操作单字节字符字符串的设计特性。
  3. string类是使用char(即作为它的字符类型,使用它的默认char_traits和分配器类型(关于模板的更多信息,请参阅basic_string)。
  4. string类是basic_string模板类的一个实例,它使用char来实例化basic_string模板类,并用char_traits和allocator作为basic_string的默认参数(根于更多的模板信息请参考basic_string)。
  5. 注意,这个类独立于所使用的编码来处理字节:如果用来处理多字节或变长字符(如UTF-8)的序列,这个类的所有成员(如长度或大小)以及它的迭代器,将仍然按照字节(而不是实际编码的字符)来操作。

总结:

  1. string是表示字符串的字符串类
  2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。
  3. string在底层实际是:basic_string模板类的别名,typedef basic_string<char, char_traits, allocator>string;
  4. 不能操作多字节或者变长字符的序列。

string类的常用接口说明

1、string类对象的常见构造

(constructor)函数名称功能说明
string();构造空的string类对象,即空字符串
string(const string & str);拷贝构造函数
string(const string & str, size_t pos, size_t len = npos);使用sttring类对象,从pos位置开始,长度为len的字符串来构造。
string(const char* s);用C-string来构造string类对象
string(const char* s, size_t n);用C-string的前n个字符来构造
string(size_t n, char c);string类对象包含n个字符c
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1;
	string s2("hello world");
	string s3(s2, 6, 3);
	string s4(s2, 6);
	string s5("hello world", 3);
	string s6(10, '*');

	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	cout << s5 << endl;
	cout << s6 << endl;

	return 0;
}

2、string类对象的容量操作

函数名称功能说明
size返回字符串有效字符长度
length返回字符串有效字符长度
capacity返回空间总大小
empty检测字符串释放为空串,是返回true,否则返回false
clear 清空有效字符
reserve为字符串预留空间**
resize将有效字符的个数该成n个,多出的空间用字符c填充
	string s1("hello world");
	cout << s1.size() << endl;
	cout << s1.length() << endl; // 与size用法相同
	cout << s1.max_size() << endl;
	cout << s1.capacity() << endl;
	cout << s1.empty() << endl;
	s1.clear();
	
int main()
{
	string s1("hello world");
	string s2("hello world");
	// 扩容
	s1.reserve(100);
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;

	// 扩容+初始化
	s2.resize(100, '*'); // 会将size与capacity都改变,并初始化为0。
	cout << s2.size() << endl;
	cout << s2.capacity() << endl;

	// 比size小可以删除数据,但是不会改变capacity,因为缩容一般不会原地缩,会有空间的开辟与释放
	s2.resize(5);
	cout << s2.size() << endl;
	cout << s2.capacity() << endl;
}

注意:

  1. size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()。
  2. clear()只是将string中有效字符清空,不改变底层空间大小。
  3. resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变。
  4. reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间总大小时,reserver不会改变容量大小。

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

函数名称功能说明
operator[] 返回pos位置的字符,const string类对象调用
begin+ endbegin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器
rbegin + rendbegin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器
范围forC++11支持更简洁的范围for的新遍历方式
int main()
{
	string s1("hello world");

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

	for (auto ch : s1)
	{
		cout << ch << endl;
	}
	cout << endl;

    // 反向迭代器
	auto it2 = s1.rbegin(); // 可以使用auto自动推导
	while (it2 != s1.rend())
	{
		cout << *it2 << " ";
		it2++;
	}
	cout << endl;
}
void func(const string& s)
{
	// 只能遍历和读容器的数据,不能写
	string::const_iterator it = s.begin();
	while (it != s.end())
	{
		cout << *it << " ";
		//*it += 1;
		++it;
	}
	cout << endl;

	auto rit = s.rbegin(); // 可以使用auto自动推导
	while (rit != s.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;
}

4、string类对象的修改操作

函数名称功能说明
push_back在字符串后尾插字符c
append在字符串后追加一个字符串
operator+= 在字符串后追加字符串str
c_str返回C格式字符串
find + npos从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置
rfind从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置
substr在str中从pos位置开始,截取n个字符,然后将其返回
insert在字符串某个位置插入字符或字符串
erase在字符串某个位置擦除字符或字符串
	string s2("hello");
	s2.push_back(' ');
	s2.push_back('!');
	cout << s2 << endl;

	s2.append("world");
	cout << s2 << endl;

	string s3("hello");
	s3 += " !world";
	cout << s3 << endl;
	// insert/erase不推荐经常使用,能少用就少用
	// 因为他们可能都存在需要挪动数据效率低下
	string s1("world");
	s1.insert(0, "hello");
	cout << s1 << endl;
	s1.insert(0, "hello");
	cout << s1 << endl;
	s1.insert(5, " ");
	cout << s1 << endl; 
	s1.insert(s1.begin()+5, ' ');
	cout << s1 << endl;
	
	string s2("hello world");
	//s2.erase(5, 1);
	s2.erase(s2.begin() + 5);
	cout << s2 << endl;
	//s2.erase(5, 30);
	s2.erase(5);
	cout << s2 << endl;
// 查找空格并替换为%20
	string s1("hello world i love you");
	size_t num = 0;
	for (auto ch : s1)
	{
		if (ch == ' ')
			++num;
	}
	// 提前开空间,避免repalce时扩容
	s1.reserve(s1.size() + 2 * num);

	size_t pos = s1.find(' ');
	while (pos != string::npos)
	{
		s1.replace(pos, 1, "%20");
		pos = s1.find(' ', pos + 3);
	}
	cout << s1 << endl;

	// 另一种方法
	string s1("hello world i love you");
	string newStr;
	size_t num = 0;
	for (auto ch : s1)
	{
		if (ch == ' ')
			++num;
	}
	// 提前开空间,避免repalce时扩容
	newStr.reserve(s1.size() + 2 * num);

	for (auto ch : s1)
	{
		if (ch != ' ')
			newStr += ch;
		else
			newStr += "%20";
	}

	s1 = newStr;
	cout << newStr << endl;
	// 两种不同的交换函数
    string s1("*****");
	string s2("xxxxx");

	// string类中的交换,只需要交换s1与s2的指针
	s1.swap(s2);
	cout << s1 << endl;
	cout << s2 << endl;
	
	// 模板泛型,需要开辟一个临时变量
	swap(s1, s2);
	cout << s1 << endl;
	cout << s2 << endl;
string类中的swap函数与std中的swap函数
cout << s1 << endl;
cout << s1.c_str() << endl;
s1 += '\0';
s1 += '\0';
s1 += "xxxxx";
cout << s1 << endl; // 按照流插入打印
cout << s1.c_str() << endl; // 按照c字符打印,返回C形式的字符串
// substr
string file("string.cpp.tar.zip");
size_t pos = file.rfind('.');
if (pos != string::npos)
{
	//string suffix = file.substr(pos, file.size() - pos);
	string suffix = file.substr(pos);
	cout << suffix << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值