美文网首页
Leetcode 刷题笔记

Leetcode 刷题笔记

作者: Hana酱酱酱 | 来源:发表于2016-10-01 16:57 被阅读0次

—删除vector中任意一个元素

使用erase函数:

iterator erase(iterator_Where);

vector<int> 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<vector<int>> result;

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

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

max 函数//

max(int,int);

max<long> (long,long);

相关文章

网友评论

      本文标题:Leetcode 刷题笔记

      本文链接:https://www.haomeiwen.com/subject/uowryttx.html