boost的函数:is_permutation

本文介绍了Boost算法库中的is_permutation函数,该函数用于判断两个容器区间内的元素是否构成排列。文章通过示例展示了如何使用此函数,并解释了不同重载版本的功能区别。
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 <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 );

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

简介:比较两个容器区间的元素是否一致(不是相等)。


示例:

int myints_1[] = {0, 1, 2, 3, 14, 15};
    int myints_2[] = {15, 14, 3, 1, 2};

    vector<int> c1(myints_1, myints_1 + sizeof(myints_1) / sizeof(int));
    vector<int> c2(myints_2, myints_2 + sizeof(myints_2) / sizeof(int));

    boost::algorithm::is_permutation ( c1.begin(),     c1.end (), c2.begin()); //false
    boost::algorithm::is_permutation ( c1.begin() + 1, c1.end (), c2.begin()); //true

    boost::algorithm::is_permutation ( c1.end (), c1.end (), c2.end()); //true

注意:

下列函数的头文件在boost\algorithm\cxx14
template< class ForwardIterator1, class ForwardIterator2, class BinaryPredicate >
bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
                      ForwardIterator2 first2, ForwardIterator2 last2,
                      BinaryPredicate p );

bool RS_Math::linearSolver(const std::vector<std::vector<double> >& mt, std::vector<double>& sn){ //verify the matrix size size_t mSize(mt.size()); //rows size_t aSize(mSize+1); //columns of augmented matrix if(std::any_of(mt.begin(), mt.end(), [&aSize](const std::vector<double>& v)->bool{ return v.size() != aSize; })) return false; sn.resize(mSize);//to hold the solution #if false boost::numeric::ublas::matrix<double> bm (mSize, mSize); boost::numeric::ublas::vector<double> bs(mSize); for(int i=0;i<mSize;i++) { for(int j=0;j<mSize;j++) { bm(i,j)=mt[i][j]; } bs(i)=mt[i][mSize]; } //solve the linear equation set by LU decomposition in boost ublas if ( boost::numeric::ublas::lu_factorize<boost::numeric::ublas::matrix<double> >(bm) ) { std::cout<<__FILE__<<" : "<<__func__<<" : line "<<__LINE__<<std::endl; std::cout<<" linear solver failed"<<std::endl; // RS_DEBUG->print(RS_Debug::D_WARNING, "linear solver failed"); return false; } boost::numeric::ublas:: triangular_matrix<double, boost::numeric::ublas::unit_lower> lm = boost::numeric::ublas::triangular_adaptor< boost::numeric::ublas::matrix<double>, boost::numeric::ublas::unit_lower>(bm); boost::numeric::ublas:: triangular_matrix<double, boost::numeric::ublas::upper> um = boost::numeric::ublas::triangular_adaptor< boost::numeric::ublas::matrix<double>, boost::numeric::ublas::upper>(bm); ; boost::numeric::ublas::inplace_solve(lm,bs, boost::numeric::ublas::lower_tag()); boost::numeric::ublas::inplace_solve(um,bs, boost::numeric::ublas::upper_tag()); for(int i=0;i<mSize;i++){ sn[i]=bs(i); } // std::cout<<"dn="<<dn<<std::endl; // data.center.set(-0.5*dn(1)/dn(0),-0.5*dn(3)/dn(2)); // center // double d(1.+0.25*(dn(1)*dn(1)/dn(0)+dn(3)*dn(3)/dn(2))); // if(fabs(dn(0))<RS_TOLERANCE2 // ||fabs(dn(2))<RS_TOLERANCE2 // ||d/dn(0)<RS_TOLERANCE2 // ||d/dn(2)<RS_TOLERANCE2 // ) { // //ellipse not defined // return false; // } // d=sqrt(d/dn(0)); // data.majorP.set(d,0.); // data.ratio=sqrt(dn(0)/dn(2)); #else // solve the linear equation by Gauss-Jordan elimination std::vector<std::vector<double> > mt0(mt); //copy the matrix; for(size_t i=0;i<mSize;++i){ size_t imax(i); double cmax(fabs(mt0[i][i])); for(size_t j=i+1;j<mSize;++j) { if(fabs(mt0[j][i]) > cmax ) { imax=j; cmax=fabs(mt0[j][i]); } } if(cmax<RS_TOLERANCE2) return false; //singular matrix if(imax != i) {//move the line with largest absolute value at column i to row i, to avoid division by zero std::swap(mt0[i],mt0[imax]); } for(size_t k=i+1;k<=mSize;++k) { //normalize the i-th row mt0[i][k] /= mt0[i][i]; } mt0[i][i]=1.; for(size_t j=0;j<mSize;++j) {//Gauss-Jordan if(j != i ) { double& a = mt0[j][i]; for(size_t k=i+1;k<=mSize;++k) { mt0[j][k] -= mt0[i][k]*a; } a=0.; } } //output gauss-jordan results for debugging // std::cout<<"========"<<i<<"==========\n"; // for(auto v0: mt0){ // for(auto v1:v0) // std::cout<<v1<<'\t'; // std::cout<<std::endl; // } } for(size_t i=0;i<mSize;++i) { sn[i]=mt0[i][mSize]; } #endif return true; }
最新发布
08-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值