当前位置: 首页 > 编程日记 > 正文

【C++】C++11 STL算法(七):排列操作(Permutation operations)、数值操作(Numeric operations)

排列操作(Permutation operations)

一、is_permutation

1、原型:
template< class ForwardIt1, class ForwardIt2 >
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 );template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >
bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,ForwardIt2 first2, BinaryPredicate p );
2、说明:

判读序列1是否是序列2的一种排列方式

3、官方demo
#include <algorithm>
#include <vector>
#include <iostream>
int main()
{std::vector<int> v1{1,2,3,4,5};std::vector<int> v2{3,5,4,1,2};std::cout << "3,5,4,1,2 is a permutation of 1,2,3,4,5? "<< std::boolalpha<< std::is_permutation(v1.begin(), v1.end(), v2.begin()) << '\n';std::vector<int> v3{3,5,4,1,1};std::cout << "3,5,4,1,1 is a permutation of 1,2,3,4,5? "<< std::boolalpha<< std::is_permutation(v1.begin(), v1.end(), v3.begin()) << '\n';
}

Output:

3,5,4,1,2 is a permutation of 1,2,3,4,5? true
3,5,4,1,1 is a permutation of 1,2,3,4,5? false

二、next_permutation

1、原型:
template< class BidirIt >
bool next_permutation( BidirIt first, BidirIt last );template< class BidirIt, class Compare >
bool next_permutation( BidirIt first, BidirIt last, Compare comp );
2、说明:

按字典顺序生成下一个序列,如果有返回true;如果没有,生成第一个排列序列(std::sort(first, last)),并返回false。

3、官方demo
#include <algorithm>
#include <string>
#include <iostream>int main()
{std::string s = "aba";std::sort(s.begin(), s.end());do {std::cout << s << '\n';} while(std::next_permutation(s.begin(), s.end()));
}

Output:

aab
aba
baa

三、prev_permutation

1、原型:
template< class BidirIt >
bool prev_permutation( BidirIt first, BidirIt last);template< class BidirIt, class Compare >
bool prev_permutation( BidirIt first, BidirIt last, Compare comp);
2、说明:

按字典顺序生成上一个序列,如果有返回true;如果没有,生成第一个排列序列(std::sort(first, last)),并返回false。

3、官方demo

打印abc的六种排列顺序

#include <algorithm>
#include <string>
#include <iostream>
#include <functional>
int main()
{std::string s="abc";std::sort(s.begin(), s.end(), std::greater<char>());do {std::cout << s << ' ';} while(std::prev_permutation(s.begin(), s.end()));std::cout << '\n';
}

Output:

cba cab bca bac acb abc

数值操作(Numeric operations)

一、iota

1、原型:
template< class ForwardIt, class T >
void iota( ForwardIt first, ForwardIt last, T value );
2、说明:

以递增的值填充[first, last)范围

3、官方demo
#include <algorithm>
#include <iostream>
#include <list>
#include <numeric>
#include <random>
#include <vector>int main()
{std::list<int> l(10);std::iota(l.begin(), l.end(), -4);	//以递增的值填充到链表l中std::vector<std::list<int>::iterator> v(l.size());std::iota(v.begin(), v.end(), l.begin());std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});	//std::shuffle不能操作std::list,因此使用vectorstd::cout << "Contents of the list: ";for(auto n: l) std::cout << n << ' ';std::cout << '\n';std::cout << "Contents of the list, shuffled: ";for(auto i: v) std::cout << *i << ' ';std::cout << '\n';
}

Possible output:

Contents of the list: -4 -3 -2 -1 0 1 2 3 4 5
Contents of the list, shuffled: 0 -1 3 4 -4 1 -2 -3 2 5

二、accumulate

1、原型:
template< class InputIt, class T >
T accumulate( InputIt first, InputIt last, T init );template< class InputIt, class T, class BinaryOperation >
T accumulate( InputIt first, InputIt last, T init, BinaryOperation op );
2、说明:

计算序列的累积值(连续加、连续乘等)

3、官方demo
#include <iostream>
#include <vector>
#include <numeric>
#include <string>
#include <functional>int main()
{std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};int sum = std::accumulate(v.begin(), v.end(), 0);	// 加int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>());	// 乘auto dash_fold = [](std::string a, int b) {	// 连接操作return std::move(a) + '-' + std::to_string(b);};std::string s = std::accumulate(std::next(v.begin()), v.end(),	// std::next是为了从第二个值开始,在前面加“-”std::to_string(v[0]), // 从第一个元素开始dash_fold);// 使用反向迭代器右折叠std::string rs = std::accumulate(std::next(v.rbegin()), v.rend(),std::to_string(v.back()), // 从最后一个元素开始dash_fold);std::cout << "sum: " << sum << '\n'<< "product: " << product << '\n'<< "dash-separated string: " << s << '\n'<< "dash-separated string (right-folded): " << rs << '\n';
}

Output:

sum: 55
product: 3628800
dash-separated string: 1-2-3-4-5-6-7-8-9-10
dash-separated string (right-folded): 10-9-8-7-6-5-4-3-2-1

三、inner_product

1、原型:
template< class InputIt1, class InputIt2, class T >
T inner_product( InputIt1 first1, InputIt1 last1,InputIt2 first2, T init );template<class InputIt1, class InputIt2, class T,class BinaryOperation1, class BinaryOperation2>
T inner_product( InputIt1 first1, InputIt1 last1,InputIt2 first2, T init,BinaryOperation1 op1,BinaryOperation2 op2 );
2、说明:

计算两个序列的内积:即序列1和序列2的顺序对应的元素乘积之和

3、官方demo
#include <numeric>
#include <iostream>
#include <vector>
#include <functional>
int main()
{std::vector<int> a{0, 1, 2, 3, 4};std::vector<int> b{5, 4, 2, 3, 1};int r1 = std::inner_product(a.begin(), a.end(), b.begin(), 0);std::cout << "Inner product of a and b: " << r1 << '\n';int r2 = std::inner_product(a.begin(), a.end(), b.begin(), 0,std::plus<>(), std::equal_to<>());std::cout << "Number of pairwise matches between a and b: " <<  r2 << '\n';
}

Output:

Inner product of a and b: 21
Number of pairwise matches between a and b: 2	//成对匹配的数量

四、adjacent_difference

1、原型:
template< class InputIt, class OutputIt >
OutputIt adjacent_difference( InputIt first, InputIt last, OutputIt d_first );template< class InputIt, class OutputIt, class BinaryOperation >
OutputIt adjacent_difference( InputIt first, InputIt last,OutputIt d_first, BinaryOperation op );
2、说明:

计算相邻元素之间的差值。

3、官方demo
#include <numeric>
#include <vector>
#include <array>
#include <iostream>
#include <functional>
#include <iterator>int main()
{std::vector v {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};std::adjacent_difference(v.begin(), v.end(), v.begin());for (auto n : v)std::cout << n << ' ';std::cout << '\n';// 斐波纳契数列 std::array<int, 10> a {1};adjacent_difference(begin(a), std::prev(end(a)), std::next(begin(a)), std::plus<> {});copy(begin(a), end(a), std::ostream_iterator<int> {std::cout, " "});
}

Output:

2 2 2 2 2 2 2 2 2 2
1 1 2 3 5 8 13 21 34 55

五、partial_sum

1、原型:
template< class InputIt, class OutputIt >
OutputIt partial_sum( InputIt first, InputIt last, OutputIt d_first );template< class InputIt, class OutputIt, class BinaryOperation >
OutputIt partial_sum( InputIt first, InputIt last, OutputIt d_first, BinaryOperation op );
2、说明:

以如下形式计算,并将结果保存在d_first开始的目标区域:

	*(d_first)   = *first;*(d_first+1) = *first + *(first+1);*(d_first+2) = *first + *(first+1) + *(first+2);
3、官方demo
#include <numeric>
#include <vector>
#include <iostream>
#include <iterator>
#include <functional>int main()
{std::vector<int> v = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2}; // or std::vector<int>v(10, 2);std::cout << "The first 10 even numbers are: ";std::partial_sum(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));std::cout << '\n';std::partial_sum(v.begin(), v.end(), v.begin(), std::multiplies<int>());std::cout << "The first 10 powers of 2 are: ";for (auto n : v) {std::cout << n << " ";}std::cout << '\n';
}

Output:

The first 10 even numbers are: 2 4 6 8 10 12 14 16 18 20 
The first 10 powers of 2 are: 2 4 8 16 32 64 128 256 512 1024

相关文章:

码书:入门中文NLP必备干货:5分钟看懂“结巴”分词(Jieba)

导读&#xff1a;近年来&#xff0c;随着NLP技术的日益成熟&#xff0c;开源实现的分词工具越来越多&#xff0c;如Ansj、盘古分词等。在本文中&#xff0c;我们选取了Jieba进行介绍和案例展示&#xff0c;主要基于以下考虑&#xff1a;社区活跃。截止本文发布前&#xff0c;Ji…

《你必须掌握的Entity Framework 6.x与Core 2.0》正式出版感想

前言 借书正式出版之际&#xff0c;完整回顾下从写博客到写书整个历程&#xff0c;也算是对自己近三年在技术上的一个总结&#xff0c;整个历程可通过三个万万没想到来概括&#xff0c;请耐心阅读。 写博、写书完整历程回顾 从2013年12月注册博客园账号&#xff0c;注册博客园账…

JSF实现“Hello World!”

我们编写一个在页面上显示是“Hello World! ”&#xff0c;我们至少需要编写一个Page对象和一个对应模板文件&#xff08;tml&#xff09;。 第一步&#xff0c;Page对象编写 在Tapestry5中Page是与一个页面对应的POJO对象&#xff0c;它不需要继承Tapestry框架的任何基类或实现…

《权力的游戏》最终季上线!谁是你最喜爱的演员?这里有一份Python教程 | 附源码...

译者 | 刘畅编辑 | 琥珀出品 | AI科技大本营&#xff08;id&#xff1a;rgznai100&#xff09;《权力的游戏》最终季已于近日开播&#xff0c;对于全世界翘首以待的粉丝们来说&#xff0c;其最大的魅力就在于“无法预知的人物命运”。那些在魔幻时代的洪流中不断沉浮的人们&…

【C++】C++11 STL算法(八):对未初始化内存的操作(Operations on uninitialized memory)、C库(C library)

对未初始化内存的操作&#xff08;Operations on uninitialized memory&#xff09; 一、uninitialized_copy 1、原型&#xff1a; template< class InputIt, class ForwardIt > ForwardIt uninitialized_copy( InputIt first, InputIt last, ForwardIt d_first );2、…

OSPF高级设置实现全网互通

OSPF(开放式最短路径优先)是对链路状态路由协议的一种实现&#xff0c;隶属内部网关协议&#xff08;IGP&#xff09;&#xff0c;故运作于自治系统内部(AS)。采用戴克斯特拉算法&#xff08;Dijkstras algorithm&#xff09;被用来计算最短路径树。“Cost”作为路由度量值。链…

学习PHP ?

学PHP的决定真的是好的吗&#xff1f; 不怕又再错一次了吗&#xff1f; 已经是最后的一年半上学时间了.... 真的不愿再走之前那条失败的路&#xff0c;不愿&#xff0c;真的不愿&#xff1b; 这年半无论如何都要把一样技术搞精了 一年半的时间&#xff0c;对我来讲够了....只看…

【数据库】sqlite中的限制:数据库大小、表数、列数、行数、参数个数、连接数等

目录一、参考网址二、详解1、查看、设置sqlite限制命令.limit2、SQLite中的限制汇总1&#xff09;字符串或BLOB的最大长度2&#xff09;最大列数3&#xff09;SQL语句的最大长度4&#xff09;联接中的最大表数5&#xff09;表达式树的最大深度6&#xff09;函数的最大参数个数7…

flutter中的生命周期

前言 和其他的视图框架比如android的Activity一样&#xff0c;flutter中的视图Widget也存在生命周期&#xff0c;生命周期的回调函数提现在了State上面。理解flutter的生命周期&#xff0c;对我们写出一个合理的控件至关重要。组件State的生命周期整理如下图所示&#xff1a; 大…

小鱼易连获腾讯数亿C轮投资,云视频布局产业互联网

4 月 18 日&#xff0c;小鱼易连在北京举行 “鱼腾视界 产业互联” 战略合作暨融资发布会上&#xff0c;正式宣布获得 C 轮融资&#xff0c;由腾讯领投。融得的资金将全面用于小鱼易连云视频系统在产业互联网领域的落地&#xff0c;打通企业、政府、个人三者之间的柔性生态全产…

异步IO一定更好吗?

http://cnodejs.org/blog/?p1015续&#xff1a;异步IO一定更好吗&#xff1f;我之前的一篇文章《异步IO一定更好吗&#xff1f;》中举了一个很变态的例子&#xff0c;用以说明在单碟机械式硬盘上异步IO反而可能降低性能的问题&#xff0c;大家的讨论很热烈。前天的NodeParty杭…

谈谈Python那些不为人知的冷知识(二)

本文转载自Python的编程时光&#xff08;ID:Python-Time&#xff09;小明在日常Code中遇到一些好玩&#xff0c;冷门的事情&#xff0c;通常都会记录下来。从上一篇的分享来看&#xff0c;仍然有不少 Pythoner 对这些冷知识存在盲区&#xff0c;所以今天迎来第二篇。如果上篇你…

前端每日实战:45# 视频演示如何用纯 CSS 创作一个菱形 loader 动画

效果预览 按下右侧的“点击预览”按钮可以在当前页面预览&#xff0c;点击链接可以全屏预览。 https://codepen.io/comehope/pen/eKzjqK 可交互视频教程 此视频是可以交互的&#xff0c;你可以随时暂停视频&#xff0c;编辑视频中的代码。 请用 chrome, safari, edge 打开观看。…

【数据库】SQLite和MySQL之间的对比和选择

目录1、各自特定2、使用场景3、选择哪个1、各自特定 SQLite &#xff1a;独立、简单&#xff08;零配置&#xff09;&#xff1b;适用于为单个应用程序和设备提供本地数据存储。 MySQL&#xff1a;可伸缩、高并发性&#xff1b;适用于客户端/服务器模式企业数据的共享数据存储…

MySql中管理百万级要注意些什么东西(转载)

一、我们可以且应该优化什么&#xff1f; 硬件 操作系统/软件库 SQL服务器(设置和查询) 应 用编程接口(API) 应用程序 二、优化硬件 如果你需要庞大的数据库表 (>2G)&#xff0c;你应该考虑使用64位的硬件结构&#xff0c;像Alpha、Sparc或即将推出的IA64。因为MySQL内部使用…

【数据库】sqlite3数据库备份、导出方法汇总

【数据库】sqlite3常用命令及SQL语句 目录1、直接拷贝数据库2、使用.backup .clone1&#xff09;交互式2&#xff09;脚本3、导出到csv文件中&#xff08;其它格式类似&#xff09;1&#xff09;交互式2&#xff09;脚本3&#xff09;导出成其它格式汇总a> .mode asciib>…

高通与苹果宣布“复合”,英特尔黯然退场 | 极客头条

作者 | 郭芮转载自公众号CSDN&#xff08;ID:CSDNnews&#xff09;为期两年的苹果高通“诉讼之争”经历了各种推波助澜愈演愈烈&#xff0c;俨然到了最为关键的白热化阶段&#xff0c;没成想&#xff0c;在刚刚正式进入美国司法庭审环节的两天后却被强势叫停了&#xff01;4 月…

MQTT 协议 Client ID 长度不能超过23个字符

今天遇到一个MQTT的问题&#xff0c;MqttException: MQIsdp ClientId > 23 bytes ClientId的长度大于23时&#xff0c;无法链接MQTT服务器。 经过查看协议发现&#xff1a;客户端标识符(Client ID)是介于1和23个字符长度,客户端到服务器的唯一标识。它必须在搜有客户端连接到…

【数据库】适用于SQLite的SQL语句(一)

目录一、统计函数二、表TABLE1、创建表CREATE TABLE2、更改表ALTER TABLE3、删除表DROP TABLE三、分析表ANALYZE四、附加数据库 ATTACH DATABASE五、事务六、核心函数七、索引INDEX1、创建索引&#xff1a;CREATE INDEX2、查看索引&#xff1a;3、使用索引 INDEXED BY4、删除索…

谷歌大神Jeff Dean点赞网红博士论文:改进分布式共识机制 | 技术头条

作者 | Heidi Howard编译 | 刘静本文转载自公众号图灵TOPIA&#xff08;ID:turingtopia&#xff09;本文作者Heidi Howard&#xff0c;是剑桥大学计算机科学与技术系系统研究小组的分布式系统研究员。Heidi的研究领域一直围绕分布式系统中的一致性&#xff0c;容错性和性能并且…

使用Nginx做前端服务器时让Apache得到真实IP的方法

一&#xff1a;nginx.conf proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 其实这个proxy.conf里面默认都有&#xff0c;在nginx.conf使用include proxy.conf就可以 二&#xff1a;apa…

Hadoop生态圈-hive五种数据格式比较

Hadoop生态圈-hive五种数据格式比较 作者&#xff1a;尹正杰 版权声明&#xff1a;原创作品&#xff0c;谢绝转载&#xff01;否则将追究法律责任。

华为巨资收购为云计算趟平道路?

华为巨资收购为云计算趟平道路&#xff1f;<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />文 小刀马众所周知&#xff0c;华为在全球的技术能力和市场地位也是有目共睹的&#xff0c;这是华为多少年兢兢业业守成的一种回报。更…

【数据库】适用于SQLite的SQL语句(二)

目录九、视图VIEW1、创建视图2、删除视图十、虚拟表1、创建虚拟表2、删除虚拟表十一、时间和日期的函数十二、分析和故障排除十三、SQL语句中的表达式1、运算符2、字面值3、参数十四、插入 INSERT十五、SQLite关键字十六、解决冲突 ON CONFLICT九、视图VIEW 视图是基于真实数据…

从对ML一窍不通到斩获AT等special offer,拿下大厂算法岗就靠它了

整理 | 一一出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;2019 年春招就要过去&#xff0c;秋招也就不远了。对于很多计算机专业的毕业生来说&#xff0c;大部分都还处于迷茫期&#xff0c;由于大学时的大部分时间都可能在划水&#xff0c;导致不知道现在如何准备就…

WWDC2018总结

本人的第一篇文章&#xff08;现在写文章是为了提升自己的语句表达能力&#xff09; 欢迎大家观看本文章&#xff0c;是略微总结一下WWDC2018发布的iOS12的新东西 iOS12略微总结&#xff08;持续更新。。。&#xff09; iOS12 变化 iOS 12新功能汇总&#xff08;后面希望可以上…

make报错:/usr/bin/ld: cannot find -lXXX

在编译php时报错如下&#xff1a; # make 。。。 /usr/bin/ld: cannot find -lltdl collect2: ld returned 1 exit status make: *** [libphp5.la] Error 1 问题原因&#xff1a; 该问题一般是由于ld在进行库的连接时找不到库文件所致&#xff1a; 解决方案&#xff1a; 出现该…

for死循环、怪异字符串、两次return……Python冷知识(三)

本文转载自Python编程时光&#xff08;ID:Python-Time&#xff09;冷知识系列&#xff0c;已经更新至第三篇。前两篇传送门小明给你准备好了&#xff0c;还没阅读的可以学习一下。谈谈 Python 那些不为人知的冷知识&#xff08;一&#xff09;谈谈 Python 那些不为人知的冷知识…

snmpd 子代理模式编译测试

1、参考链接 1&#xff09;Net-snmp添加子代理示例https://blog.csdn.net/eyf0917/article/details/395466512、操作步骤1&#xff09;网络拷贝下面的文件http://www.net-snmp.org/tutorial/tutorial-5/toolkit/mib_module/NET-SNMP-TUTORIAL-MIB.txthttp://www.net-snmp.org/t…

【数据库】适用于SQLite的SQL语句(三)

目录十七、重新引索REINDEX十八、查询SELECT1、简单查询2、复合查询十九、更新UPDATE二十、公用表表达式&#xff08;CTE&#xff09;WITH1、普通表达式2、递归表达式二十三、VACUUM二十四、UPSERT十七、重新引索REINDEX REINDEX命令用于从头开始删除和重新创建索引。 十八、…