c++ stl 常用的4个函数

本文详细介绍了C++标准库中的四个常用算法:find用于查找指定值并返回迭代器,count用于计算指定值在区间内的出现次数,sort对区间内的元素进行排序,reverse则实现区间元素的反转。每个算法都提供了实现示例和使用方法,包括对非容器数据类型和容器类型的处理,帮助读者深入理解这些基础但重要的算法。
摘要由CSDN通过智能技术生成

算法

1.find()

查找指定值,并返回迭代器

函数原型

template <class InputIterator, class T>
   InputIterator find (InputIterator first, InputIterator last, const T& val);

用法:find (起始位置,结束位置,搜索值)

实现

template<class InputIterator, class T>
  InputIterator find (InputIterator first, InputIterator last, const T& val)
{
  while (first!=last) {
    if (*first==val) return first;
    ++first;
  }
  return last;
}

Example:

对于非容器数据类型:搜索区间:[start,end] , end 要在预期的位置+1

如果 end 与 该变量的长度相等,那么无论有没有搜索到最后一个元素,都将放回最后一个元素的迭代器

int main(){
    int a[]={1,2,3,4,5,6,7};
    auto start = a;
    auto end = a + 5;
    int* b = find(start,end,7);
    if(b==a+5){
        cout<<"can't find"<<endl;
    }
    else{
        cout<<"find"<<endl;
    }
    return 0;
}

对于容器

vector<int>v;
void test(int val){
    auto it = find(v.begin(),v.end(),val);
    if(it!=v.end()){
        cout<<"find"<<endl;
    }
    else{
        cout<<"can't find"<<endl;
    }
}
int main(){
    for(int i=0;i<5;i++){
        v.push_back(i+1);
    }
    test(6);
    test(5);
    return 0;
}

can’t find
find

2.count()

在指定区间内对指定值进行计数

函数原型

template <class InputIterator, class T>
  typename iterator_traits<InputIterator>::difference_type
    count (InputIterator first, InputIterator last, const T& val);

用法:count(起始位置,结束位置,需要计数的值)

实现

template <class InputIterator, class T>
  typename iterator_traits<InputIterator>::difference_type  //告诉编译器这是一个类型,否则编译不通过
    count (InputIterator first, InputIterator last, const T& val)
{
  typename iterator_traits<InputIterator>::difference_type ret = 0;
  while (first!=last) {
    if (*first == val) ++ret;
    ++first;
  }
  return ret;
}

Example:

int main(){
    int a[]={1,2,3,5,5,6};
    auto start = a;
    auto end = a + 6;
    cout<<count(start,end,5)<<endl;
    return 0;
}

2

对于容器

vector<int>v;
void test(){
    for(auto i = v.begin(); i != v.end(); i++){
        cout<<*i<<" : "<<count(v.begin(),v.end(),*i)<<endl;
    }
}
int main(){
    v.push_back(1);
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    test();
    return 0;
}

1 : 2
1 : 2
2 : 1
3 : 1

泛型为<int,int>

vector<pair<int,int>>v;
void test(){
    for(auto i=v.begin();i!=v.end();i++){
        cout<<"("<<i->first<<","<<i->second<<")"<<" : "<<count(v.begin(),v.end(),*i)<<endl;
    }
}
int main(){
    v.push_back(make_pair(1,1));
    v.push_back(make_pair(2,2));
    v.push_back(make_pair(2,2));
    v.push_back(make_pair(3,3));
    test();
    return 0;
}

(1,1) : 1
(2,2) : 2
(2,2) : 2
(3,3) : 1

3.sort()

将指定区间的元素进行排序

函数原型

//default (1)	使用 < 排序 : 默认从小到大
template <class RandomAccessIterator>
  void sort (RandomAccessIterator first, RandomAccessIterator last);

//custom (2)	使用Compare排序
template <class RandomAccessIterator, class Compare>
  void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

用法:sort(起始位置,结束位置) : 对 [first , last-1] 的区间进行排序

Example

void print(int* array ,int len){
    for(int i=0;i<len;i++){
        cout<<array[i]<<" ";
    }
    cout<<endl;
}
int main(){
    int a[]={1,7,5,4,6,2};
    print(a,6);
    sort(a,a+6);
    print(a,6);
    return 0;
}

1 7 5 4 6 2
1 2 4 5 6 7

如果是 sort ( a , a+5 )

( 1 4 5 6 7 ) 2

使用 Compare 排序

Compare

Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.

二元函数,接受范围中的两个元素作为参数,并返回可转换为bool的值。 返回的值指示作为第一个参数传递的元素是否被认为在它定义的特定严格弱顺序的第二个之前。

函数不能修改任何参数。

它可以是函数指针,也可以是函数对象。

void print(int* array ,int len){
    for(int i=0;i<len;i++){
        cout<<array[i]<<" ";
    }
    cout<<endl;
}
bool com(int a,int b){
    return a>b;//只有a>b,b才能排在a前
}
int main(){
    int a[]={1,7,5,4,6,2};
    print(a,6);
    sort(a,a+6,com);
    print(a,6);
    return 0;
}

1 7 5 4 6 2
7 6 5 4 2 1

4.reverse()

将指定区间的元素反转

函数原型

template <class BidirectionalIterator>
  void reverse (BidirectionalIterator first, BidirectionalIterator last);

实现

template <class BidirectionalIterator>
  void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
  while ((first!=last)&&(first!=--last)) {
    std::iter_swap (first,last);
    ++first;
  }
}

用法:reverse(起始位置,结束位置) : 对 [first , last-1] 的区间进行反转

Example

void print(int* array ,int len){
    for(int i=0;i<len;i++){
        cout<<array[i]<<" ";
    }
    cout<<endl;
}
int main(){
    int a[] = {1,2,3,4,5};
    print(a,5);
    reverse(a,a+4);
    print(a,5);
    return 0;
}

1 2 3 4 5
( 4 3 2 1 ) 5

对于容器

int main(){
    vector<int>v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    for(auto i = v.begin(); i != v.end() ; i++){
        cout<<*i<<" ";
    }
    cout<<endl;
    reverse(v.begin(),v.end());
    for(auto i = v.begin(); i != v.end() ; i++){
        cout<<*i<<" ";
    }
    return 0;
}

1 2 3 4
4 3 2 1

参考cpp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值