STL整理

/*
整理一些STL
*/
#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <unordered_map>//bits/stdc++没有这个,要用得加
#include <unordered_set>//bits/stdc++没有这个,要用得加

using namespace std;

void inAndOut()
{
	//cin,cout
	int a;
	scanf("%d", &a);
	printf("%d\n", a);
	cin >> a;//从cin写到a里
	cout << a << endl;//从a写到cout里输出
	int b, c, d;
	cin >> b >> c >> d;
	cout << b << c << d<<endl;
}

void arraySort()
{
	int a[] = { 2,1,5,0,-1,5,9 };
	sort(a, a + 7);//sort默认升序
	for (int i = 0; i < 7; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}

void stringOutput()
{
	//char *ch = "asdfghjkl"; //新标准不允许这样做
	char t[] = "asdfghjkl";
	char* ch = t;
	for (int i = 0; ch[i] != '\0'; i++)
		cout << *(ch + i);
	cout << endl;

	string s = "asdfghjkl";
	cout << s << endl;

	string s1;
	getline(cin, s1);//获取一行数据
	cout << s1;
}

void stringAdd()
{
	string s;
	s += "hello";
	s += " world";
	s += '5';
	s += 10;//10会转化成对应的asc码即换行符
	int a = 5;
	s += (a + '0');
	cout << s;
}

void stringSort()
{
	string s = "5418340";
	sort(s.begin(), s.end());
	cout << s;
}

void stringErase()
{
	string s = "5418340";
	s.erase(s.begin());
	s.erase(--s.end());//end指向结束符,减1才是最后一个元素
	cout << s;
}

void stringCut()
{
	//substr函数
	string s = "123456";
	s = s.substr(1, 3);
	cout << s << endl;
	s = s.substr(1, -1);//截到最后
	cout << s << endl;
}

void stringCycle()
{
	string a = "123456";
	for (int i = 0; i < a.length(); i++)
	{
		cout << a[i] << endl;
	}
	for (string::iterator it = a.begin(); it != a.end(); it++)
	{
		cout << *it << endl;
	}
	for (auto it = a.begin(); it != a.end(); it++)
	{
		cout << *it << endl;
	}
	//C++11新特性
	for (auto x : a)
		cout << x << endl;
}

void vectorUse()
{
	vector<int> v;//定义一个空vector
	vector<int> v2(4);//定义1个大小为4的vector,初始为0
	vector<int> v3(4, 6);//定义1个大小为4的vector,初始为6
	vector<int> v4{ 1,2,3,4,5 };//定义一个vector,数字为1,2,3,4,5
	for (auto x : v4)
		cout << x << endl;
	cout << v4[1] << endl;//取索引为1的值
	cout << v4.at(2) << endl;//取索引为2的值

	v.push_back(5);//push_back尾部追加
	v.push_back(4);
	v.push_back(7);
	for (auto x : v)
		cout << "v push_back:"<<x << endl;

	v4.resize(10);//将v4 从大小为5 重置为 大小为10
	for (auto x : v4)
		cout << "v4 resize:" << x << endl;

	v.erase(v.begin());
	v.erase(--v.end());
	for (auto x : v)
		cout << "v erase:" << x << endl;

	cout << v4.front() << endl;//和queue一样可以front和back
	cout << v4[0] << endl;
	cout << *v4.begin() << endl;

	cout << v4.back() << endl;
	cout << v4[v4.size() - 1] << endl;
	cout << *--v4.end() << endl;


}

void vectorSortAndCycle()
{
	vector<int> v{ 5,1,2,5,4,0,-1 };

	sort(v.begin(), v.end(), less<int>());//第三个参数不写也默认less
	for (auto x : v)
		cout << x;
	cout << endl;
	sort(v.begin(), v.end(), greater<int>());
	for (auto x : v)
		cout << x;
	cout << endl;
	sort(v.begin(), v.end());

	for (int i = 0; i < v.size(); i++)
		cout << v[i];
	cout << endl;
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
		cout << *it;
	cout << endl;
	for (auto it = v.begin(); it != v.end(); it++)
		cout << *it;
	cout << endl;
	for (auto x : v)
		cout << x;
}

void stackUse()
{
	stack<int> s;
	s.push(2);//类比vector的push_back
	s.push(3);
	cout << s.top() << endl;
	s.pop();
	cout << s.top() << endl;
	cout << s.size() << endl;
}

//进制转换(十进制转二进制)【用栈做】【转三四五六等等进制都行】
void ten2TwoByStack()
{
	int decimal=8;
	stack<int> s;
	int res = 0;
	while (decimal != 0)
	{
		s.push(decimal % 2);
		decimal /= 2;
	}
	while (!s.empty())
	{
		res = res * 10 + s.top();
		s.pop();
	}
	cout << res << endl;
}

//逆序打印【用栈做】
void reversePrint()
{
	string str;
	stack<string> s;
	getline(cin, str);
	stringstream ss;
	ss << str;//对stringstream来说,<<是一直往字符串流中写任意字符
	while (ss >> str)//ss碰到空格符会截断放入到str,空格符不属于任何str
	{
		//cout << str << " " << str.length() << endl;
		s.push(str);
	}
	while (!s.empty())
	{
		cout << s.top();
		s.pop();
		if (s.size() != 0)//字符串用length,栈用size
			cout << " ";
	}
}


//字符串转数字
void string2Num()
{
	//方法一:直接通过ss转换
	string s = "1234";
	int i;
	stringstream ss;
	ss << s;
	ss >> i;
	cout << i;

	cout << endl << endl;
	//方法二:用stoi函数转化,速度比用stringstream更快
	int j = stoi(s);
	cout << j << endl;
}
//数字转字符串
void num2String()
{
	//方法一
	int a = 3456;
	string out;
	stringstream ss;
	ss << a;
	ss >> out;
	cout << out << endl;

	cout << endl << endl;

	cout << to_string(a) << endl;
}

//queue的操作和stack一样,只是实现思想不一样
//stack是从后面进从后面出,queue是从后面进从前面出
void queueUse()
{
	queue<int> q;
	q.push(5);
	q.push(6);
	cout << q.front() << endl;
	cout << q.back() << endl;
	cout << "size:" << q.size() << endl;
	q.pop();
	cout << q.front() << endl;
	cout << q.back() << endl;
	cout << "size:" << q.size() << endl;
}

//map是键默认升序的,unordered_map是无序的,unordered_multimap是允许用重复键的map
//map表示映射,map是树状表,unordered_map为哈希表
void mapUse()
{
	map<int, int> m;
	m[6] = 3;
	m[5] = 8;
	m[8] = 9;
	for (auto it = m.begin(); it != m.end(); it++)
		cout << it->first << " " << it->second << endl;
	for (auto tmp : m)//这里tmp是pair型,不是指针,不用—>用.
		cout << tmp.first << " " << tmp.second << endl;

	cout << endl;
	
	unordered_map<int, int> q;
	q[6] = 3;
	q[5] = 8;
	q[9] = 9;
	for (auto it = q.begin(); it != q.end(); it++)
		cout << it->first << " " << it->second << endl;
	for (auto tmp : q)
		cout << tmp.first << " " << tmp.second << endl;
}

//pair的用法(map转成vector进行排序)
int cmp1(pair<int, int> a, pair<int, int> b)
{
	return a.first > b.first;
}
int cmp2(pair<int, int> a, pair<int, int> b)
{
	return a.first < b.first;
}
int cmp3(pair<int, int> a, pair<int, int> b)
{
	return a.second > b.second;
}
int cmp4(pair<int, int> a, pair<int, int> b)
{
	return a.second < b.second;
}

void mapReSort()
{
	unordered_map<int, int> m;
	m[6] = 3;
	m[7] = 8;
	m[3] = 9;
	vector<pair<int, int>> v(m.begin(), m.end());
	sort(v.begin(), v.end(), cmp1);
	for (auto tmp : v)
		cout << tmp.first << tmp.second << endl;
	cout << endl;
	sort(v.begin(), v.end(), cmp2);
	for (auto tmp : v)
		cout << tmp.first << tmp.second << endl;
	cout << endl;
	sort(v.begin(), v.end(), cmp3);
	for (auto tmp : v)
		cout << tmp.first << tmp.second << endl;
	cout << endl;
	sort(v.begin(), v.end(), cmp4);
	for (auto tmp : v)
		cout << tmp.first << tmp.second << endl;
	cout << endl;
}

//集合(有序、计数、去重)
void setUse()
{
	set<int> s;
	unordered_set<int> s2;
	s.insert(3);
	s.insert(4);
	s.insert(4);
	s.insert(4);
	cout << s.size() << endl;
	for (auto tmp : s)
		cout << tmp << " ";
	cout << endl;
	for (auto it = s.begin(); it != s.end(); it++)
		cout << *it << " ";
	cout << endl;
}
//双端队列 前面进出 后面进出 都自由
void dequeUse()
{
	deque<int> d;
	//4 9 1 2
	d.push_back(1);
	d.push_back(2);
	d.push_front(9);
	d.push_front(4);
	for (auto tmp : d)
		cout << tmp << endl;
	cout << endl;
	d.pop_back();
	d.pop_front();
	sort(d.begin(), d.end(), greater<int>());
	sort(d.begin(), d.end(), less<int>());
	for (auto it = d.begin(); it != d.end(); it++)
		cout << *it << endl;

}

/*
C++11中,针对顺序容器(如vector、deque、list),
新标准引入了三个新成员:emplace_front、emplace和emplace_back,
这些操作分别对应push_front、insert和push_back,
允许我们将元素放置在容器头部、一个指定位置之前或容器尾部。

当调用push或insert成员函数时,
我们将元素类型的对象传递给它们,
这些对象被拷贝到容器中。
而当我们调用一个emplace成员函数时,
则是将参数传递给元素类型的构造函数。
emplace成员使用这些参数在容器管理的内存空间中直接构造元素。

emplace相关函数可以减少内存拷贝和移动,效率更高
*/
//list 双向链表
void listUse()
{
	list<int> li;
	li.push_back(6);
	li.push_front(5);
	for (auto tmp : li)
		cout << tmp << endl;
	cout << endl;
	li.emplace_back(9);
	li.emplace_front(10);
	for (auto tmp : li)
		cout << tmp << endl;
	cout << endl;
	li.insert(++li.begin(), 2);
	li.emplace(++li.begin(), 1);
	for (auto it = li.begin(); it != li.end(); it++)
		cout << *it << endl;
}


int main()
{
	//inAndOut();
	//arraySort();
	//stringOutput();
	//stringAdd();
	//stringSort();
	//stringErase();
	stringCut();
	//stringCycle();
	//vectorUse();
	//vectorSortAndCycle();
	//stackUse();
	//ten2TwoByStack();
	//reversePrint();
	//string2Num();
	//num2String();
	//queueUse();
	//mapUse();
	//mapReSort();
	//setUse();
	//dequeUse();
	//listUse();
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值