1. 初始化
int a[10]={1}
int a[]={1,2,3,4,5}
int a[2][5]={{1,2,3,4,5},{6,7,8,9,10}}
int *a = (int*)malloc(sizeof(int)*10)
int *a = new int(10)
struct TreeNode {
TreeNode& operator+=(const TreeNode& node){
return *this;
}
}
typedef pair<int,int> pii;
vector<int> a(10)
vector<int> a(10,1)
vector<int> a; a.assign(10,1)
vector<int> a(b)
vector<int> a(b,b+3)
vector<int> a(b.begin(),b.begin()+3)
vector<vector<int>> vec(N,vector<int>(M,value))
string str = "hello"
string str("hello")
string str = str0
string str(str0)
string str(10,'h')
string str = string(10,'h')
string str(str0,6)
string str(str0,0,5)
2. 容器/数据结构用法
vec.push_back(elem)
vec.pop_back()
vec.front()
vec.back()
vec.size()
vec.empty()
vec[i]
vec.resize(length)
vec.clear()
map<int,int> m
m[key] = value
m.erase()
for(auto it = v1.begin(); it != v1.end(); it++)
it->first,it->second;
大顶堆 priority_queue <int,vector<int>,less<int> >
小顶堆 priority_queue <int,vector<int>,greater<int> >
priority_queue< Node, vector<Node>, cmp> q;
struct cmp
{
bool operator () (const Node &a, const Node &b) {return a.weight < b.weight;}
};
push(elem)
pop()
insert(elem)
erase(elem)删除元素
push_back(elem)
pop_back()
push_front(elem)
pop_front()
str.size()
str[i],str.at(i)
str.insert(pos,s)
str.replace()
str.append(4,'h')
str.append(str0)
str.append(str0,4,6)
str.assign()
str.erase(0)
str.erase(0,1)
str.erase(str.end()-3, str.end())
str.substr(1)
str.substr(0,3)
str.find(str0)
str.push_back()
str.pop_bacK()
3. 常用函数和操作
sort(s.begin(),s.end(),cmp)
sort(nums.begin(),nums.end(),greater<int>())
sort(nums.begin(),nums.end(),[](vector<int>& n1, vector<int>& n2){
return n1[1]<n2[1]
});
翻转:reverse(nums.begin(),nums.end())
求和: accumulate(nums.begin(),nums.end(),0)
去重(先排序): unique(nums.begin(),nums.end())
二分查找:lower_bound()
二分查找:upper_bound()
atoi(s1.c_str())
to_string(a)
floor()向下取整、ceil()向上取整、round()四舍五入
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本