博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【C++】C++11 STL算法(七):排列操作(Permutation operations)、数值操作(Numeric operations)
阅读量:4262 次
发布时间:2019-05-26

本文共 7665 字,大约阅读时间需要 25 分钟。

排列操作(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 
#include
#include
int main(){
std::vector
v1{
1,2,3,4,5}; std::vector
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
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? true3,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 
#include
#include
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:

aabababaa

三、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 
#include
#include
#include
int main(){
std::string s="abc"; std::sort(s.begin(), s.end(), std::greater
()); 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 
#include
#include
#include
#include
#include
int main(){ std::list
l(10); std::iota(l.begin(), l.end(), -4); //以递增的值填充到链表l中 std::vector
::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,因此使用vector std::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 5Contents 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 
#include
#include
#include
#include
int main(){
std::vector
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
()); // 乘 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: 55product: 3628800dash-separated string: 1-2-3-4-5-6-7-8-9-10dash-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
T inner_product( InputIt1 first1, InputIt1 last1, InputIt2 first2, T init, BinaryOperation1 op1, BinaryOperation2 op2 );
2、说明:

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

3、官方demo
#include 
#include
#include
#include
int main(){
std::vector
a{
0, 1, 2, 3, 4}; std::vector
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: 21Number 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 
#include
#include
#include
#include
#include
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
a { 1}; adjacent_difference(begin(a), std::prev(end(a)), std::next(begin(a)), std::plus<> { }); copy(begin(a), end(a), std::ostream_iterator
{ std::cout, " "});}

Output:

2 2 2 2 2 2 2 2 2 21 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 
#include
#include
#include
#include
int main(){
std::vector
v = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}; // or std::vector
v(10, 2); std::cout << "The first 10 even numbers are: "; std::partial_sum(v.begin(), v.end(), std::ostream_iterator
(std::cout, " ")); std::cout << '\n'; std::partial_sum(v.begin(), v.end(), v.begin(), std::multiplies
()); 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

转载地址:http://kdmei.baihongyu.com/

你可能感兴趣的文章
Elasticsearch、Logstash 、kibana安装配置
查看>>
Spring Boot 解决跨域问题的 3 种方案!
查看>>
SQL 性能优化,开发注意事项
查看>>
logback配置文件模板
查看>>
指针函数和冒泡排序法算法案例
查看>>
批处理修改地址为静态和动态的方法
查看>>
easyui $.messager.alert失效问题
查看>>
android studio 删除module
查看>>
android jsbridge实现原理简述
查看>>
java继承中的重写和隐藏
查看>>
java内部类为什么可以访问外部类属性方法
查看>>
StringBuilder StringBuffer 如何清空
查看>>
android软键盘弹起面试题
查看>>
HashMap实现原理和扩容及高版本优化
查看>>
java集合之LinkedHashMap解析
查看>>
Messenger和aidl的关系&aidl支持多线程吗,messenger呢
查看>>
AndroidManifest.xml文件何时被加载?如何查看apk的AndroidManifest
查看>>
如何判断一个对象是否可回收,GC回收对象的过程方式,finilized函数
查看>>
java普通for循环和增强for循环中做集合增删会不会出错?
查看>>
抽象类和接口区别
查看>>