【STL】string类 (上)& <vector>和<list>的简单使用

目录


一,什么是 STL 

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

二,STL 的六大组件

三,标准库中的 string 类

string 类的介绍:

https://cplusplus.com/reference/string/string/?kw=string

1,string 类 

总结:

1,string 是表示字符串的字符串类

2,该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作 string 的常规操            作。

3,string 在底层实际是:basic_string 模板类的别名,typedef basic_string string;

4,不能操作多字节或者变长字符的序列。

在使用 string 类时,必须包含 #include 头文件以及 using namespace std;

2,string 类的常用接口

1,string类对象的常见构造

详情:cplusplus.com/reference/string/string/string/

函数名称功能说明
string()构造空的 string 类对象,即空字符串
string(const string& str)拷贝构造函数
string (const string& str, size_t pos, size_t len = npos);
截取从 pos 开始 npos 长度的字符串
string (const char* s );用C-string 来构造 string 类对象
string (const char* s,size_t n);截取字符串前 n 个字符
string (size_t n,char c);

string 类对象中包含 n 个字符c

2,string(const string& str)

拷贝构造函数

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

int main()
{
	string s1 = "abc";
	string s2(s1);
	cout << s2 << endl;
	return 0;
}

记得加上头文件  #include<string> ;

3,string (const string& str, size_t pos, size_t len = npos);

截取从 pos 开始 npos 长度的字符串

int main()
{
	string s1 = "hello world";
	string s3 (s1, 2, 5);
	cout << s3 << endl;
	
	string s4(s1, 0, 10);
	cout << s4 << endl;

	string s5(s1, 3);
	cout << s5 << endl;

	return 0;
}

第一个数:目标字符串

第二个数:代表下标,从下标位置开始;

第三个数:代表长度,如果没写的话就一直读取到 ‘ \0 ‘ 为止;

4,string (const char* s )

用C-string 来构造 string 类对象

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

	string s2("hahaha 666");
	cout << s2 << endl;
	return 0;
}

5,string (const char* s,size_t n);

截取字符串前 n 个字符

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

	string s2("hahaha 666",2);
	cout << s2 << endl;
	return 0;
}

第一个数:要写双引号字符串形式的,要不然会和string (const string& str, size_t pos, size_t                      len = npos);起冲突;

第二个数:代表读取的个数,从头开始;

6,string (size_t n,char c);

string 类对象中包含 n 个字符c

int main()
{
	string s1(10,'x');
	cout << s1 << endl;

	string s2(5,'a');
	cout << s2 << endl;
	return 0;
}

3,遍历和访问

int main()
{
	string s1("hello world");
	cout << s1.size() << endl;
	cout << s1.length() << endl;

	int i = 0;
	for (i = 0; i < s1.size(); i++)
	{
		cout << s1[i] <<" ";
	}

	return 0;
}

s1.size() 和 s1.length 是求字符串长度的;

访问时可以直接像数组一样用 [ 下标 ] 的形式;

四,iterator 迭代

string 类给我们提供了一个 迭代 iterator,来帮助我们进行遍历;

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

	return 0;
}

string::iterator 是一个类型,it 可以把它看作是一个指针;

s1.begin() 就是相当于第一个元素的指针地址,s1.end() 相当于最后 \0 的地址;

五,逆置字符串 reverse

我们先来逆置一个字符串

int main()
{
	string s1("hello world");
	int begin = 0, end = s1.size()-1;
	while (begin< end)
	{
		int tmp = s1[begin];
		s1[begin] = s1[end];
		s1[end] = tmp;
		begin++;
		end--;
	}
	cout << s1 << endl;
	return 0;
}

 reverse 逆置字符串

int main()
{
	string s1("hello world");
	reverse(s1.begin(), s1.end());
	cout << s1 << endl;
	return 0;
}

是不是简单多了;

六,<vector> 栈

我们写栈再也不用手搓了,直接用c++库里面的栈就可以了,需要包含头文件 #include<vector>;

int main()
{
	vector<int> s1;
	s1.push_back(1);
	s1.push_back(2);
	s1.push_back(3);
	s1.push_back(4);
	s1.pop_back();

	vector<int>::iterator st = s1.begin();
	while (st != s1.end())
	{
		cout << *st << " ";
		st++;
	}
	return 0;
}

而且迭代 iterator 对栈也一样有用;

对逆置函数 reverse 也一样有效果;

int main()
{
	vector<int> s1;
	s1.push_back(1);
	s1.push_back(2);
	s1.push_back(3);
	s1.push_back(4);
	s1.pop_back();
	reverse(s1.begin(), s1.end());

	vector<int>::iterator st = s1.begin();
	while (st != s1.end())
	{
		cout << *st << " ";
		st++;
	}
	return 0;
}

七,队列 <list>

我们以后也不用手搓队列了,c++库里面也有队列,我们直接用即可,需要包含头文件 #include<list>;

int main()
{
	list<int> sl;
	sl.push_back(1);
	sl.push_back(2);
	sl.push_back(3);
	sl.push_back(4);
	sl.pop_back();
	
	list<int>::iterator lt = sl.begin();
	while (lt != sl.end())
	{
		cout << *lt << " ";
		lt++;
	}
	return 0;
}

也是一样的用法,也同样适用于 迭代 iterator;

逆置 reverse 函数也是OK的;

int main()
{
	list<int> sl;
	sl.push_back(1);
	sl.push_back(2);
	sl.push_back(3);
	sl.push_back(4);
	sl.pop_back();
	
	reverse(sl.begin(), sl.end());

	list<int>::iterator lt = sl.begin();
	while (lt != sl.end())
	{
		cout << *lt << " ";
		lt++;
	}
	return 0;
}

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2023年12月11日
下一篇 2023年12月11日

相关推荐