STL学习笔记
hp_cpp
这个作者很懒,什么都没留下…
展开
-
利用std::map和std::greater对key进行降序排列
std::map默认是用std::less对key进行升序排列的,利用std::greater可以对key进行降序排序。在Qt中,如果我们需要对QString或者int为key的map进行降序排列,除了自定义类型外,其实还可以利用std::map和std::greater实现代码:#include <QCoreApplication>#include <map>#include <algorithm>#include <QDebug>int原创 2020-10-26 17:54:30 · 3215 阅读 · 0 评论 -
std::map删除某个或者某些元素的示例
#include <iostream>#include <map>#include <string>using namespace std;int main(){ typedef std::map<string, int> StringIntMap; StringIntMap coll; coll["aa"] = 1; coll["bb"] = 2; coll["cc"] = 1; coll["dd"] = 2; int value原创 2020-10-21 21:35:13 · 2394 阅读 · 0 评论 -
C++标准库(第2版)第7章map的一个运行期指定排序规则的示例
《C++标准库(第2版)》【德】Nicolai M.Josuttis著,侯捷 译这本书,第7.8.6的一个综合实例:运用Map、String并于运行期指定排序规则该例子展现了以下技巧:如何使用map,包括使用关联式数组(associative array)接口。如何撰写和使用函数对象(function object)。如何在运行期定义排序规则(sorting criterion)。如何在“大小写无关”的情况下比较字符串(string)。#include <iostream>#原创 2020-10-21 21:08:41 · 139 阅读 · 0 评论 -
STL算法之lexicographical_compare的基本用法
我们这里只掌握lexicographical_compare的基本用法:需要包含头文件 algorithm(1)template <class InputIterator1, class InputIterator2>bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIt原创 2020-10-21 20:54:31 · 2695 阅读 · 1 评论 -
std::list::remove_if的用法
vs中拷贝出来的 template<class _Pr1> void remove_if(_Pr1 _Pred) { // erase each element satisfying _Pred _Remove_if(_Pred); } template<class _Pr1> void _Remove_if(_Pr1& _Pred) ...原创 2020-03-24 09:45:12 · 901 阅读 · 0 评论 -
std::accumulate的使用以及注意点
头文件 #include <numeric>定义,(1) template< class InputIt, class T > T accumulate( InputIt first, InputIt last, T init ); (2) template< class InputIt, class T, class BinaryOperation &...原创 2020-01-08 20:55:59 · 1129 阅读 · 0 评论 -
std::iota实现递增序列(c++11新增)
原生数组和vector、array都不支持构造一个递增的数组,比如一个包含0~999的size为1000的数组。int ids[1000] = {};for (int i = 0; i < 1000; ++i){ ids[i] = i;}如果是std::vector,则是:std::vector<int> ids;ids.resize(1000);for (i...原创 2020-01-08 10:11:12 · 5113 阅读 · 0 评论 -
STL算法之std::partition和std::stable_partition的使用
在序列中分区元素会重新对元素进行排列,所有使给定谓词返回 true 的元素会被放在所有使谓词返回 false 的元素的前面。这就是 std::partition和std::stable_partition算法所做的事。从名字上就可以看得出来 std::partition是不稳定的,而std::stable_partition是稳定的。代码示例一:#include <algorithm&...原创 2020-01-07 10:11:22 · 1054 阅读 · 0 评论 -
Bind2nd源码解析
例如:#include <iostream>#include <vector>#include <functional>#include <algorithm>using namespace std;int main(int argc, char *argv[]){ vector<int> coll{ 1, 3, 5, ...原创 2019-08-15 23:01:44 · 214 阅读 · 0 评论