Boost 学习之算法篇 is_permutation

15 篇文章 0 订阅

原文链接:http://www.boost.org/doc/libs/1_60_0/libs/algorithm/doc/html/the_boost_algorithm_library/CXX11/is_permutation.html

is_permutation

       头文件 'is_permutation.hpp' 包含单个is_permutation 算法的六个不同变种. 该算法测试一个序列是否是另一个序列的置换;换句话说第二个序列是否以不同的顺序包含了第一个序列的所有成员。
       常用的is_permutatoon 带了两个参数序列以及一个可选的谓词。假如两个序列包含相同的元素,则返回true。假如函数带了一个谓词,函数使用谓词来比较参数序列的元素,看他们是否相等。
       is_permutation 有三种调用形式。 第一种形式带有一对迭代器,来表示第一个参数序列范围以及第二个参数序列范围的起始位置;
第二种形式带两个参数来定义序列一的范围,以及另外两个迭代器来定义第二个参数序列的范围;第三种形式带了一个使用Boost.Range遍历过的单参数。


官方API

template< class ForwardIterator1, class ForwardIterator2 >
bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2 );

template< class ForwardIterator1, class ForwardIterator2, class BinaryPredicate >
bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
                      ForwardIterator2 first2, BinaryPredicate p );


template< class ForwardIterator1, class ForwardIterator2 >
bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2 );

template< class ForwardIterator1, class ForwardIterator2, class BinaryPredicate >
bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
                      ForwardIterator2 first2, ForwardIterator2 last2,
                      BinaryPredicate p );

template <typename Range, typename ForwardIterator>
bool is_permutation ( const Range &r, ForwardIterator first2 );

template <typename Range, typename ForwardIterator, typename BinaryPredicate>
bool is_permutation ( const Range &r, ForwardIterator first2, BinaryPredicate pred );

      一般来说,你应该使用四个迭代器版本函数。带三个迭代器版本的函数不得不内部调用std::advance(first2, std::distance(first1,last1))的创建第四个参数,同时,假如第二个序列比第一个短,那么这是一个未定义的行为。

举例详解

假如有一个容器c1 包含{0,1,2,3,14,15},以及容器c2 包含{15,14,3,1,2}

is_permutation ( c1.begin(),     c1.end (), c2.begin(), c2.end ())返回false。因为c2并不包含c1的所有元素。
is_permutation ( c1.begin() + 1, c1.end (), c2.begin(), c2.end ()) 返回true。因为此时第二个序列以反序方式包含了第一个序列的所有元素
is_permutation ( c1.end (), c1.end (), c2.end(), c2.end ())返回ture,空范围都返回ture

迭代器要求 (这句话没办法翻译(⊙o⊙)…))
     is_permutation works on forward iterators or better.

时间复杂度
is_permutation 的所有四个变种的时间复杂度都是 O(N^2);也就是说,他们比较序列中的每一个元素N次 。 如果传递的是随机访问地带器,则is_permutation发现两个序列长度不一致的话将会马上返回。


异常安全

is_permutation的所有变种函数都是按值调用参数,不依赖任何全局的状态。因此,这些函数都提供了很强的异常安全保证。
注意

    The three iterator versions of the routine is_permutation are part of the C++11 standard. When compiled using a C++11 implementation, the implementation from the standard library will be used.
    
    The four iterator versions of the routine is_permutation are part of the proposed C++14 standard. When C++14 standard libraries become available, the implementation should be changed to use the implementation from the standard library (if available).
    
    假如传递的参数序列是两个空范围,不论谓词是什么,都返回true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值