lower_bound、upper_bound以及unqiue、erase函数的用法

函数返回值

lower_bound函数以及upper_bound函数返回的是一个interator,它指向在区间[first,last)标记的有序序列中可以插入value、此解释来自于百度百科

unique函数去掉容器中的相邻的重复元素,要求容器中的元素一定要有序,所以一般在使用函数unique的时候,都要对数组或者容器中的元素进行排序,函数不是真正的去除重复,函数返回的是去重后的尾地址,在返回的尾地址之后就是重复元素(将重复的元素放到数组的后方)

erase函数表示删除迭代器所指示的某一位置或者某一区间的元素,返回值是一个迭代器,指向被删除元素后面的元素

函数解释

lower_bound返回的是不小于value的第一个位置,这个位置就是value插入的位置

upper_bound返回的是大于value的第一个位置

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#define ll long long
using namespace std;
vector<int> v;
int a[16] = {1,1,1,2,3,4,5,6,7,7,7,8,8,8,9,9};
int main()
{
    cout<<"lower_bound"<<endl;
    for(int i = 1;i <= 10;i ++)
        cout<<lower_bound(a,a+16,i)-a<<endl;       //第一个等于等于他的位置所在

    cout<<"upper_bound"<<endl;
    for(int i = 1;i <= 10;i ++)
        cout<<upper_bound(a,a+16,i)-a<<endl;        //第一个大于他的位置所在
}

我们这里查看一下lower_bound的源码便知:

template <class ForwardIterator, class T, class Distance>
ForwardIterator __lower_bound(ForwardIterator first, ForwardIterator last,
                              const T& value, Distance*,
                              forward_iterator_tag) {   //这里的迭代器至少为单项迭代器
  Distance len = 0;
  distance(first, last, len);
  Distance half;
  ForwardIterator middle;

  while (len > 0) {
    half = len >> 1;
    middle = first;
    advance(middle, half);               //得到middle,也就是first向后偏移half长度
    if (*middle < value) {               //在后半部分中
      first = middle;
      ++first;                          
      len = len - half - 1;
    }
    else                               
      len = half;                        
  }
  return first;
}

这里不用迭代器直接模拟一下:

#include <iostream>
#include <functional>
#include <algorithm>

using namespace std;

template <class Iterator,class T>
Iterator _lower_bound(Iterator first,Iterator last,const T& value){
    int len = last - first;
    Iterator middle;
    int half;
    while(len > 0){
        half = len >> 1;
        middle = first + half;
        if(*middle < value){
            first = middle + 1;
            len = len - half - 1;
        }
        else
            len = half;
    }
    return first;
}

int main()
{
    int a[] = {0,1,2,3,4,6,7,8,9,10};

    cout<<*_lower_bound(a,a+10,5)<<endl;;
    return 0;
}

unique函数去除相邻的重复元素,返回值是去重后的尾地址,但是后面的元素还是和之前数组或者容器中的元素是相同的

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#define ll long long
using namespace std;
vector<int> v;

int main()
{
    int a[10] = {1,1,2,2,3,3,4,4,5,5};
    int n = unique(a,a+10)-a;
    for(int i = 0;i < n;i ++)
        cout<<a[i]<<" ";
    cout<<endl;
    for(int i = 0;i < 10;i ++)  //这里n后面的元素和原始数组中的相同
        cout<<a[i]<<"  ";
    cout<<endl;
}

erase函数有两种形式:

iterator erase(iterator position);      //删除某一指定位置上的元素
iterator erase(iterator first,iterator last);   //删除某一区间上的元素
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#define ll long long
using namespace std;
vector<int> v;

int main()
{
   int a[10] = {1,1,1,2,2,3,3,4,5,6};
    for(int i = 0;i < 10;i ++)
        v.push_back(a[i]);
    v.erase(unique(v.begin(),v.end()),v.end());
    vector<int>::iterator it;
    for(it = v.begin();it != v.end();it ++)
        cout<<*it<<" ";
    cout<<endl;
}


 

猜你喜欢

转载自blog.csdn.net/li1615882553/article/details/80928488