4 functions commonly used in C ++ STL

tags: C ++ knowledge  c++  algorithm  stl  C language

algorithm

1.find()

Find the specified value and return to the iterator

Function prototype

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

Usage: Find (start location, end position, search value)

accomplish

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:

For non -container data types:Search range: [start, end], END should be at the expected location +1

If END is equal to the length of the variable, no matter whether the last element is searched, it will be placed back to the iterator of the last element

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;
}

Container

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()

Dig the specified value in the specified interval

Function prototype

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

Usage: Count (starting position, ending position, the value of counting)

accomplish

template <class InputIterator, class T>
  typename iterator_traits<InputIterator>::difference_type  // Tell the compiler this is a type, otherwise the compilation will not pass
    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

Container

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

Facial style <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()

Sort the elements in the specified interval

Function prototype

// Default (1) Use <Satellite: default from small to large
template <class RandomAccessIterator>
  void sort (RandomAccessIterator first, RandomAccessIterator last);

// Custom (2) Sorting with compare
template <class RandomAccessIterator, class Compare>
  void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

Usage: sort

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

If it is sort (a, a+5)

( 1 4 5 6 7 ) 2

Use compare to sort

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.

The binary function, the two elements in the acceptance range are used as the parameter, and returned the value that can be converted to BOOL. The returned value indicates whether the element of the first parameter transmission is considered before the second one of the specific strict weak order it is defined.

Functions cannot modify any parameters.

It can be a function pointer or a function object.

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;// Only a> b can be ranked in front of 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()

Reverse the elements in the specified interval

Function prototype

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

accomplish

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

Usage: Reverse

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

Container

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

Reference CPP

Intelligent Recommendation

C++ template library STL-commonly used functions under algorithm

Follow the book to make a finishing: 0、max(),min(),abs() Must be an integer in abs(); if you want to find the absolute value of floating point, use fabs() 1. swap (very convenient~) 2. reverse (revers...

C++ stl string overview and detailed analysis of commonly used functions

string   Overview: Definition of string Access to string: Introduction to commonly used functions of string: operator += : Join two strings together Comparison operator: Compare the size of two string...

C++STL (4) Introduction to commonly used containers (3) stack, queue

1. Stack container Introduction Stack: Stack is a first in last out (FILO) data structure, it has only one outlet, as shown in the figure. The stack container allows adding elements, removing elements...

C++ commonly used functions

Clock(), CLOCKS_PER_SEC test function run time Clock() is a timing function in C/C++. The function returns the CPU clock timing unit (clock tick) from "starting this program process" to &quo...

c ++ commonly used functions

    Common C ++ library functions   As shown, a common mathematical functions shown in FIG. Header #include <math> or #include <math.h>   Function prototype Features re...

More Recommendation

C # commonly used functions

1.BitConverter Action: The base type value into a byte value, the byte value or a converted value based on the type of Liezi:   2.MemoryStream   3.FileStream   4.File   5.BinaryRea...

Commonly used functions c++

for(int i=1;i<=10;i++){          cout<<string(i,'#')<<endl;    }     vector<int>ve; ve.resize(n+1); for(int i=1;i<=n;i++)...

C# commonly used functions

C# write txt text line C# get current directory  ...

On the C ++ STL containers commonly used in

STL is a C / C ++ development of a very important templates, and various containers in which the definition is very convenient to all of us to use. Here, we On some commonly used containers. Here we a...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top