复赛笔记(有问题请私信)

· · 算法·理论

//一、常用函数。 

//1.swap()交换两个变量的值
#include <algorithm>
using namespace std;
int a=10,b=20;
int main(){swap(a,b);}//现在a=20,b=10

//2.max和min
//max:返回两个值中的较大值
//min:返回两个值中的较小值
#include <algorithm>
using namespace std;
int a=5,b=10;
int main()
{
    mnans=min(a,b)//5
    mxans=max(a,b)//10
}

//3.fill()设置容器中所有元素为指定值
#include <algorithm>
#include <vector>
using namespace std;
vector<int> vec(10);
int main(){fill(vec.begin(),vec.end(),5);}//vec的所有元素为5 

//4.atoi()将C风格字符串转化为整数 
#include <cstdlib>
using namespace std;
const char* str="12345";
int main(){int num=atoi(str);}//num=12345

//5.stoi()将string转化为整数
#include <string> 
using namespace std;
string s="12345";
int main(){int num=stoi(s);}//num=12345

//6.count()统计指定值在范围内出现的次数
#include <algorithm>
#include <vector>
using namespace std;
vector<int> vec={1,2,2,3,4,2};
int main(){int cnt=count(vec.begin(),vec.end(),2);}//cnt=3

//7.lower_bound()与upper_bound
//lower_bound:在有序范围内查找一个不小于给定值的元素
//upper_bound:在有序范围内查找一个大于给定值的元素
//基于二分查找实现 
#include <algorithm>
#include <vector>
using namespace std;
vector<int> vec={1,2,4,4,5,6};
int main()
{
    auto lb=lower_bound(vec.begin(),vec.end(),4);//指向的一个4
    auto ub=upper_bound(vec.begin(),vec.end(),4);//指向的一个5
    //如果要直接输出结果,前面要加星号。如:
    cout<<*lower_bound(vec.begin(),vec.end(),4);
    //或:
    cout<<*upper_bound(vec.begin(),vec.end(),4);
}

//8.reverse()反转指定范围内的元素顺序
#include <algorithm>
#include <vector> 
vector<int> vec={1,2,3,4,5};
int main(){reverse(vec.begin(),vec.end());}//vec变为{5,4,3,2,1}

#include <algorithm>
#include <string>
using namespace std;
string s="hello";
int main(){reverse(s.begin(),s.end());}//s变为"olleh"

//9.sort()对数组、vector、list进行排序
#include <algorithm>
#include <vector> 
#include <iostream> 
using namespace std;
int main()
{
    vector<int> vec={4,2,3,5,1};
    sort(vec.begin(),vec.end());
    for(int num : vec)
    {
        cout<<num<<" ";
    }
    return 0;
    //输出:1 2 3 4 5 
}

#include <algorithm>
#include <vector> 
#include <iostream> 
using namespace std;
int main()
{
    vector<int> vec={4,2,3,5,1};
    sort(vec.begin(),vec.end(),greater<int>());
    for(int num : vec)
    {
        cout<<num<<" ";
    }
    return 0;
    //输出:5 4 3 2 1 
}

//二、数据结构。

//1.list
//底层:双向链表
//插入:push_back(),push_front(),irser()
//删除:pop_back(),pop_front(),erase(),femove() 
//查找:find()(需要配合<algorithm>库) 
//清空:clear()
//判断空:empty()
//遍历:使用迭代器begin()到end() 

//2.vector
//底层:动态数组 
//插入:push_back(),insert()
//删除:pop_back(),erase(),clear() 
//查找:find()(需要配合<algorithm>库) 
//清空:clear()
//判断空:empty()
//遍历:使用迭代器begin()到end()

//3.stack
//底层:栈 
//插入:push()
//删除:pop()
//访问栈顶:top() 
//判断是否为空:empty()
//大小:size()

//4.queue
//底层:deque(双端队列)
//插入:push()
//删除:pop()
//访问队首:front() 
//判断是否为空:empty()
//大小:size()

//5.priority_queue
//底层:堆(默认最大堆)
//插入:push()
//删除:pop()
//访问最大元素:top() 
//判断是否为空:empty()
//大小:size()

//6.map/multimap
//底层:红黑树 
//插入:insert(),operator[]
//删除:erase(),clear()
//查找:find()
//判断是否存在:使用find()返回的迭代器
//清空:clear()
//判断空:empty() 
//统计元素个数:count()(map只返回0或1)
//遍历:使用迭代器 begin()到end() 

更新中......