c++11 algorithm 常用的容器算法
来源:互联网 发布:狸窝转换器for mac 编辑:程序博客网 时间:2024/05/21 10:35
c++的vector、list、set、map及其成员函数已经足够我们大多数应用了,
但是,
c++11增加的algorithm配合container可谓所向披靡。
vector<int> vec = { 1, 2, 3, 4, 5, 6 }, out;
std::copy_if:将容器中符合要求的元素复制到另一个容器
std::copy_if(vec.begin(), vec.end(), std::back_inserter(out), // 在out的结尾插入。如果是开头,使用std::front_inserter [](int it)->bool { return it % 2 == 1; });std::remove_if:将待删除的元素全都移动到容器的尾部
auto iterator = std::remove_if(vec.begin(), vec.end(), [](int it)->bool { return it % 2 == 1; });
此时容器内容为2,4,6,1,3,5,iterator指向1
vec.erase(iterator, vec.end()); // 删除1,3,5
为什么std::remove_if没有删除元素而是仅将待删除元素移到容器尾部呢?
因为删除元素会导致容器发生变化,如vector会拷贝元素,这将大大影响效率。
std::reverse:将容器的元素逆转
std::sort:排序
auto compare = []()->bool {};
返回true,表示升序排列;返回false表示降序排列。
特别注意:比较函数必须确定,不能出现二义性(某些情况a大于b,另些情况a小于b)。
更多算法请参见:http://www.cplusplus.com/reference/algorithm/
1 0
- c++11 algorithm 常用的容器算法
- 常用算法(Algorithm)的用法介绍
- algorithm中常用的现成算法
- C++:STL常用函数模块总结(算法algorithm)
- 容器常用函数、algorithm头文件
- C++语法基础--泛型算法(generic algorithm)--写入容器元素的算法fill(),fill_n,replace()
- C++语法基础--泛型算法(generic algorithm)--对容器排序的算法sort(),stable_sort(),unique()
- (C#)排序算法 Sort Algorithm
- 哈夫曼算法(huffman algorithm C)
- C的常用算法
- C的常用算法
- C++/C++11中<algorithm>的使用
- 【STL】<algorithm><numeric><functional> 中的常用算法
- [Algorithm] 机器学习算法常用指标总结
- algorithm下的常用函数
- [C++]容器和算法
- C程序设计的常用算法
- C程序设计的常用算法
- 最新App Store审核10大被拒理由
- 51nod 1090 3个数和为0
- dev 错误窗口打开
- Java对BigDecimal常用方法的归类
- 关于nodejs接受多文件上传问题
- c++11 algorithm 常用的容器算法
- 解决[PUUIAlbumListViewController shouldAutorotate] is returning YES
- 错误模块名称: KERNELBASE.dll
- 《Windows程序设计》读书笔二 Unicode简介
- 简单播放视频,使用摄像头拍照
- android xampp study ---1
- 让gradle执行cmd 或者其他exe命令的方法
- iOS9 3D touch 适配开发
- Python案例-开发之路-文件内容查找并修改