0
点赞
收藏
分享

微信扫一扫

C++之list

目录

一,什么是 STL 

二,STL 的六大组件

三,标准库中的 string 类

1,string 类 

2,string 类的常用接口

1,string类对象的常见构造

2,string(const string& str)

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

4,string (const char* s )

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

6,string (size_t n,char c);

3,遍历和访问

四,iterator 迭代

五,逆置字符串 reverse

六, 栈

七,队列


一,什么是 STL 

二,STL 的六大组件

三,标准库中的 string 类

1,string 类 

总结:

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

2,string 类的常用接口

1,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);
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 )
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);
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);
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> 栈

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>

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;
}

举报

相关推荐

C++之list(2)

C++之list模拟实现

C++学习之list容器

C++之list的用法介绍

C++:list

0 条评论