Leetcode 刷题笔记

—删除vector中任意一个元素

使用erase函数:

iterator erase(iterator_Where);

vector vec;

vec.erase(vec.begin()+i); //删除i位置的元素

使用sort排序:

sort(vec.begin(),vec.end());(默认是按升序排列,即从小到大).

可以通过重写排序比较函数按照降序比较,如下:

定义排序比较函数:

放在main() 之前

bool Comp(const int &a,const int &b)

{

return a>b;

}

调用时:sort(vec.begin(),vec.end(),Comp),这样就降序排序。


**vector在最前面插入元素

vec.insert(vec.begin(),1); 

如果 vector> result;

result.insert(result.begin(),{}); 插入空vector没有作用

必须:result.insert(result.begin(),{1});


max 函数//

max(int,int);

max (long,long);

你可能感兴趣的:(Leetcode 刷题笔记)