2.C++STL笔记--vector、stack、queue

vector

 vector可以简单理解为数组或是向量
  1. 构造和打印
#include<iostream>
#include<vector>
using namespace std;

int main()
{
	//三种构造方式
	vector<int> v; //定义单个空vector
	vector<int> v2(4); //定义一组4单元的vector,且均初始化为0
	vector<int> v3(4,6); 定义一组4单元的vector,且均初始化为6
	for (auto x : v) cout << x;
	cout << endl;
	for (auto x : v2) cout << x;
	cout << endl;
	for (auto x : v3) cout << x;
	cout << endl;
	system("pause");
	return 0;
}

在这里插入图片描述

  1. 获取元素
#include<iostream>
#include<vector>
using namespace std;

int main()
{
	vector<int> v{1,2,3,4,5,6}; //定义单个空vector
	cout << v[2] << endl;
	cout << v.at(2) << endl;
	for (auto x : v) cout << x;
	cout << endl;
	system("pause");
	return 0;
}

在这里插入图片描述

  1. 方法
    • push_back(x) 添加后续元素
    • resize 重置大小,不赋值时默认为0
    • erase 删除元素,注意:有复杂度,复杂度为o(n)
    • front,back 获取第一个元素、最后一个元素
#include<iostream>
#include<vector>
using namespace std;

int main()
{
	vector<int> v{1,2,3,4,5,6}; //定义单个空vector
	for (auto x : v) cout << x;
	cout << endl;
	v.push_back(7); 
	v.push_back(10);
	for (auto x : v) cout << x;
	cout << endl;
	v.resize(6);  //重置为6
	for (auto x : v) cout << x;
	cout << endl;
	v.erase(v.begin()+2); //删除了3
	v.erase(--v.end()); //删除最后一个元素
	for (auto x : v) cout << x;
	cout << endl;
	cout << v.front() << endl; //取第一个
	cout << v.back() << endl;  //取最后一个
	cout << *v.begin() << endl; //取第一个
	cout << v[v.size()-1] << endl; //取最后一个
	cout << *--v.end() << endl; //取最后一个
	system("pause");
	return 0;
}

在这里插入图片描述

  1. 排序
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
	vector<int> v{7,8,5,-1,0,9}; //定义单个空vector
	for (auto x : v) cout << x;
	cout << endl;
	sort(v.begin(), v.end());  //从小到大排序,默认的是less<int>()
	for (auto x : v) cout << x;
	cout << endl;
	sort(v.begin(), v.end(),greater<int>());  //从大到小排序
	for (auto x : v) cout << x;
	cout << endl;

	system("pause");
	return 0;
}

在这里插入图片描述
5. 循环

stack

  1. 基本操作
#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;

int main()
{
	stack<int> s;  //先进后出
	s.push(2); //入栈
	s.push(3); //入栈
	s.push(4); //入栈
	cout << s.size() << endl; //堆栈元素个数
	cout << s.top() << endl; //读栈顶元素
	s.pop(); //出栈,无返回值
	cout << s.top() << endl; //读栈顶元素
	system("pause");
	return 0;
}
  • 实例子–十进制转换为任意进制
#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;

int itob(int decimal,int index);

int main()
{
	//十进制转换任意进制
	int decimal = 20;
	int dd;
	dd = itob(decimal,2);
	cout <<"二进制" << dd << endl;
	dd = itob(decimal, 8);
	cout <<"八进制" << dd << endl;
	dd = itob(decimal, 16);
	cout <<"十六进制"<< dd << endl;
	system("pause");
	return 0;
}

int itob(int decimal,int index)
{
	int ret = 0;
	stack<int> s;
	while (decimal!=0)
	{
		s.push(decimal % index);
		decimal /= index;
	}

	while (!s.empty())
	{
		ret = ret * 10 + s.top();
		s.pop();
	}

	return ret;
}

在这里插入图片描述

  • 逆序单词,堆栈做法
    如:字符串逆序打印
#include<iostream>
#include<stack>
#include<string>
#include<sstream>
using namespace std;

int main()
{
	/*第一种方法*/
	string str;
	stack<string> s;
	getline(cin, str);
	stringstream ss;
	ss << str; //字符流把str流入ss
	while (ss >> str)
		s.push(str);
	while (!s.empty())
	{
		cout << s.top();
		s.pop();
		if(s.size()>0 )
			cout << " ";
	}
	
	system("pause");
	return 0;
}

在这里插入图片描述

  • 字符串转换为数字
    2种方法
#include<iostream>
#include<string>
#include<sstream>
using namespace std;

int main()
{
	string s = "123546789";
	int i;
	stringstream ss; //即可数字转字符,也可字符转数字。
	ss << s;
	ss >> i;
	cout << i;
	
	cout << endl;
	
	j = stoi(s);  //方法二,stoi在string头文件中
	cout << j;
	
	system("pause");
	return 0;
	//cout << to_sting(i)<<endl; 数字转字符串c++11新增方法PAT支持。
}

在这里插入图片描述

queue

  1. 基本操作(队列用的比较少)
#include<iostream>
#include<queue>
using namespace std;

int main()
{
	queue<int> q;
	q.push(5);
	q.push(7);
	q.push(9);
	cout << q.front() << endl;
	q.pop();
	cout << q.front() << endl;
	cout << q.back() << endl;
	cout << q.size() << endl;

	system("pause");
	return 0;
	//cout << to_sting(i)<<endl; 数字转字符串c++11新增方法PAT支持。
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值