一些函数

next_permutation(数组名, 数组名 + 排列长度);

while(next_permutation()) 可实现对指定长度的数组全排列 

 

判断浮点数之间相等不能用 ==  采取fabs( )计算 :   fabs()计算浮点数的绝对值

    double a,b;
    cin >>  a >> b;  
    const double eps=1.0e-6;//设置绝对误差阈值,当两个数绝对误差很小很小的时候,就认为两者相等;
    if(fabs(a-b)<=eps)
    {
       cout<<"a和b相等"<<endl;
    }
    else
    {
        cout<<"a和b不相等"<<endl;
    }

 

unique( )函数,参数类型为两个迭代器

与erase( )搭配实现对两迭代器间内容去重

 

#include<iostream>
#include<algorithm>
#include<cassert>
using namespace std;
 
int main()
{
 
    vector<int> a ={1,3,3,4,5,6,6,7};
    vector<int>::iterator it_1 = a.begin();
    vector<int>::iterator it_2 = a.end();
    vector<int>::iterator new_end;
 
    new_end = unique(it_1,it_2); //注意unique的返回值
    a.erase(new_end,it_2);
    cout<<"删除重复元素后的 a : ";
    for(int i = 0 ; i < a.size(); i++)
        cout<<a[i];
    cout<<endl;
 
}

 

posted @ 2022-03-15 18:56  HM-7  阅读(36)  评论(0)    收藏  举报