比赛前用来回忆的代码不是很完整
目录
map
#include <bits/stdc++.h>
#include"head.h"
using namespace std;
int solve6()
{
//一个映射,
//map 一个元素仅出现一次,有序,默认从小到大
//multimap 一个元素可以出现多次,有序,默认从小到大
//unordered_map 一个元素仅出现一次,无序
map<string, int> mp1;
if (mp1.find("1") != mp1.end())
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
mp1.erase("1");
mp1.clear();
mp1["a"] = 1;
mp1["b"] = 2;
for (map<string, int>::iterator it = mp1.begin();it != mp1.end();it++)
{
cout << it->first << " " << it->second << endl;
}
for (auto &tonum : mp1)
{
cout << tonum.first << " " <<tonum.second<< endl;
}
return 0;
}
pair
#include <bits/stdc++.h>
#include"head.h"
using namespace std;
int solve/*8*/()
{
pair<int, int> par1 = make_pair(1, 2);
pair<int, int> par2 = { 1,2 };
if (par1 == par2)
{
cout << "YES" << endl;
cout << par1.first << par1.second << endl;
}
return 0;
}
set
#include <bits/stdc++.h>
#include"head.h"
using namespace std;
int solve5()
{
//一个集合,在找特别分散但是数量不多的数字特别好用
//set 一个元素要么在集合中,要么不在,出现一次,有序,默认从小到大
//multiset 一个元素要么在集合中,要么不在,可以出现多次,有序,默认从小到大
//unordered_set 一个元素要么在集合中,要么不在,出现一次,无序
set<int> st;
set<int, greater<int> >st1;//从大到小
//通用st.size();st.clear();st.empty();
st.insert(1);
st.insert(2);
st.insert(1);
for (auto& tonum : st)
{
cout << tonum << endl;
}
//for(set<int>::iterator it=st.begin();it!=st.end();++it)cout<<*it<<endl;
st.erase(1);
cout << st.count(1)<<endl;
cout << st.count(2) << endl;
auto so = st.find(2);//返回迭代器
cout << *so;
//unorder_set<int> st2 无序
//multiset_set<int> st3 多个数
return 0;
}
vector
#include <bits/stdc++.h>
#include"head.h"
using namespace std;
int solve2()
{
vector<int> ssr(10, 10086);//(长度,数值)
ssr.push_back(1);//开销大
ssr.pop_back();//删除尾部
ssr.clear();
ssr.empty();
ssr.size();//返回size_t,在相乘可能溢出
int num1=100;
ssr.resize(num1);//大补0,小就删
//一些如条件限制n*m的题目,用普通数组不好判断大小,就要使用vector限制
vector<vector<int> > ssr1(100, vector<int>());//行,列
for (auto& tonum : ssr)//auto
{
cout << tonum << endl;
}
return 0;
}
string
#include <bits/stdc++.h>
#include"head.h"
using namespace std;
int solve7/**/()
{
string s1(10, '0');//第一个个数,第二个赋值
string s2(10, '1');
s1[0] = '1';
string s3 = "12345678";
//out << s1 + s2 << endl;
cout << s3.substr(3,2) << endl;//第一个定位从下标3开始,第二个是子串长度,用来截取子串
if (s1.find("101")!=string::npos)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
cout << (s1.find(100)) << endl;
s1.find(100);
int ssr = stoi(s1);
string s = to_string(ssr);
//注意,尾接字符串用+=
//find效率不高
return 0;
}
stack
#include <bits/stdc++.h>
#include"head.h"
using namespace std;
int solve3()
{
stack<int> stk;
//我用的比较少,不如直接用vector
stk.push(111);
stk.push(222);
stk.push(333);
cout << stk.size() << endl;
stk.empty();
/*for (auto& tonum : stk) //不可以直接访问
cout << tonum <<endl;*/
cout<<stk.top();
stk.pop();
cout<<stk.top();
return 0;
}
queue
#include <bits/stdc++.h>
#include"head.h"
using namespace std;
int solve4()
{
//不可访问内部元素
queue <int> quq;
quq.size();
quq.empty();
quq.push(11);
quq.push(22);
quq.push(33);
cout << quq.front() << " " << quq.back() << endl;
return 0;
}
priority_queue
#include <bits/stdc++.h>
#include"head.h"
using namespace std;
struct game
{
int year;
int money;
};
struct compare_outbig {
bool operator()(const game& game1, const game& game2)
{
return game1.money < game2.money;//钱少的在堆下,多的在顶部,输出最大的
}
};
struct compare_outsmall {
bool operator()(const game& game1, const game& game2)
{
return game1.money > game2.money;//钱多的在堆下,少的在顶部,输出最小的
}
};
int solve1() {
priority_queue<int> out_big;
priority_queue<int, vector<int>, greater<int> >out_small;
priority_queue<game, vector<game>, compare_outbig> struct_out_big;
priority_queue<game, vector<game>, compare_outsmall>struct_out_small;
int n=1;
while(n)
{
cin >> n;
out_big.push(n);
out_small.push(n);
}
cout << "out_big" << endl;
while (!out_big.empty())
{
cout << out_big.top() << endl;
out_big.pop();
}
cout << "out_small" << endl;
while (!out_small.empty())
{
cout << out_small.top() << endl;
out_small.pop();
}
int m = 1;
while (m)
{
cin >> n >> m;
game sample1;
sample1.year = n;
sample1.money = m;
struct_out_big.push(sample1);
struct_out_small.push(sample1);
}
cout << "struct_out_big" << endl;
while (!struct_out_big.empty())
{
game sample1 = struct_out_big.top();
cout <<"game_money:"<< sample1.money <<"game_:" <<sample1.year << endl;
struct_out_big.pop();
}
cout << "struct_out_small" << endl;
while (!struct_out_small.empty())
{
game sample1 = struct_out_small.top();
cout << "game_money:" << sample1.money << "game_:" << sample1.year << endl;
struct_out_small.pop();
}
return 0;
}