一、各类容器及其使用方法
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
void print(int a) {
cout << a << " ";
}
int main() {
//========================vector相关方法========================
vector<int> a;
a.push_back(10); //末尾插入
a.push_back(20);
a.pop_back(); //末尾删除
a.insert(a.begin(), 40); //头插法
a.insert(a.begin() + 2, 30); //中间插入
vector<int>::iterator begin = a.begin(); //迭代器开始指针
vector<int>::iterator end = a.end(); //迭代器结束指针
for_each(begin, end, print); //循环方法
cout << "a.size():" << a.size() << endl; //size是实际存入数据空间的大小
a.resize(2); //强制更新容器长度为2,截取保留前面的
a.resize(6, 1);//强制将长度改为6,如果没有值则默认值为1
a[5] = 3;
cout << "a.capacity():" << a.capacity() << endl; //capacity是容量,即能容纳的数据个数
for_each(a.begin(), a.end(), print);
vector<int>(a).swap(a); //巧妙收缩空间
vector<int> list1 = { 1,2,3,4,5 }; //初始化赋值
//========================string相关方法========================
string str1 = "abcd";
str1 += 'a'; //字符串拼接 +=
cout << str1 << endl;
string str2 = "123";
str2 += str1;
cout << str2.compare("123abcda") << endl; //对比,相同为0,不同为1或-1
cout << "str2: " << str2 << endl;
cout << "str2.substr(1, 2):" << str2.substr(1, 2) << endl; //截取
str2.insert(2, "--"); //插入
cout << "str2.insert(2, '--'):" << str2 << endl;
str2.erase(1, 2); //删除指定位置字符串
cout << "str2.erase(1, 2):" << str2 << endl;
//========================deque相关方法========================
deque<int> deque1; //deque是双端队列,Vector容器是单向开口的连续内存空间
deque1.push_front(1); //头部插入
deque1.pop_front();
deque1.push_back(2); //尾部插入
deque1.pop_back();
//========================stack相关方法========================
stack<int> stack1; //不支持循环访问、不支持随机存取
stack1.push(1); //进栈
stack1.push(2); //进栈
stack1.pop(); //出栈
cout << stack1.top() << endl; //栈顶元素
cout << stack1.empty() << endl; //判断是否为空
//========================queue相关方法========================
queue<int> queue1; //队列,先进先出
queue1.push(1);
queue1.push(2);
queue1.pop();
cout << queue1.front() << endl; //队头元素
cout << queue1.back() << endl; //队尾元素
//========================list相关方法========================
list<int> arr = { 1,2,3,4,5 }; //双向链表
//========================set/multiset相关方法========================
set<int> set1 = {1, 24, 4, 4, 4, 4, 5}; //set不允许重复元素,会自动过滤掉重复元素,默认从小到大(底层实现为红黑树)
multiset<int> multiset1; //multiset可以保存重复的元素
set1.insert(1);
set1.insert(1);
set1.insert(2);
cout << "set1.size():" << set1.size() << endl;
for_each(set1.begin(), set1.end(), print);//遍历
cout << endl;
for (set<int>::iterator it = set1.begin(); it != set1.end(); it++) { //另外一种遍历的方法
cout << *it << " ";
}
cout << endl;
set1.erase(++set1.begin()); //按迭代器删除
set1.erase(24); //按值删除
for(set<int>::iterator it = set1.begin(); it != set1.end(); it++){
cout << *it << ' ';
}
set1.find(4);
set1.lower_bound(2); //找到第一个大于等于key的元素,没有查到则返回set1.end(),查找到了返回迭代器
set1.upper_bound(2); //找到第一个大于key的元素
cout << endl;
set<int, less<int>> set2 = {1,6,8,14,10,2}; //由小到大排序
for_each(set2.begin(), set2.end(), print);
cout << endl;
set<int, greater<int>> set3 = { 1,6,8,14,10,2 }; //由大到小排序
for_each(set3.begin(), set3.end(), print);
//========================pair相关方法========================
pair<int, int> pair1(10, 20); //构造方法1
cout << endl;
cout << pair1.first << " " << pair1.second << endl;
pair<int, string> pair2 = make_pair(10, "abcd"); //构造方法2
cout << pair2.first << " " << pair2.second << endl;
pair<int, string> pair3 = pair2; //构造方法3
//========================map相关方法========================
map<int, string> map1; //第一个参数key的类型,第二个参数value的类型
//第一种
map1.insert(pair<int, string>(0, "student_zero"));
//第二种
map1.insert(make_pair(1, "student_one"));
//第三种
map1.insert(map<int, string>::value_type(2, "student_two"));
//第四种
map1[10] = "student_ten";
for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++) {
cout << "key:" << (*it).first << ' ' << "value:" << it->second << endl;
}
map<string, string> map2;
map2["name"] = "laowang";
map2["account"] = "wiasjdf451212";
cout << map2["student"] << endl; //如果用[]访问map中不存在的key,那么map会自动将其插入到map中,并赋默认值
for (map<string, string>::iterator it = map2.begin(); it != map2.end(); it++) {
cout << "key:" << (*it).first << ' ' << "value:" << it->second << endl;
}
//两种map按key查找的方法
if (map2.count("name")) {
cout << map2["name"] << endl;
}
if (map2.find("account") != map2.end()) {
cout << map2.find("account")->second << endl;
}
return 0;
}
二、STL常用库函数(要引入方法库algorithm) 1、sort函数
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool mysort(int a, int b) {
return a > b;
}
int main() {
//当对数组进行排序时
int arr[6] = { 10,2,32,4,99,1 };
sort(arr, arr + 6); //默认升序排列
sort(arr, arr + 6, mysort); //可以传入自定义函数改变排序规则
for (int i = 0; i < 6; i++)
{
cout << arr[i] << ' ';
}
cout << endl;
//当对标准模板对象进行排序时
vector<int> list = { 10,2,32,4,99,1 };
sort(list.begin(), list.end());
for (int i = 0; i < list.size(); i++)
{
cout << list[i] << ' ';
}
}
2、next_permutation函数 用途:求全排列的
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> list = { 1,2,3,4 };
sort(list.begin(),list.end());
do {
for (int i = 0; i < list.size(); i++)
{
cout << list[i] << ' ';
}
cout << endl;
} while (next_permutation(list.begin(), list.end()));
}
法二:利用递归求全排列: 输入数组:[1, 2, 3] 返回数组:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,2,1],[3,1,2]]
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void dfs(vector<vector<int>>& res, vector<int>& num, int index) {
if (index == num.size() - 1) res.push_back(num); //当进入到尾部时找到一种排列直接将其插入到结果数组中
else {
for (int i = index; i < num.size(); i++) { //遍历后续元素
swap(num[index], num[i]); //交换二者
dfs(res, num, index + 1); //继续递归往后找
swap(num[index], num[i]); //回溯,将位置还原,保证下次换位置不受影响
}
}
}
vector<vector<int> > permute(vector<int>& num) {
sort(num.begin(), num.end()); //按字典序排序
vector<vector<int>> res; //定义结果数组
dfs(res, num, 0); //递归
return res;
}
int main() {
vector<int> list = { 1,2,3,4 };
vector<vector<int>> res = permute(list);
for (int i = 0; i < res.size(); i++)
{
for (int j = 0; j < res[i].size(); j++)
{
cout << res[i][j] << ' ';
}
cout << endl;
}
}
3、swap函数
vector<int> res = {1,2,3,4};
swap(res[0], res[3]); //交换
swap(res[1], res[2]);
|