STL (STL containers using timing, function object, predicate)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_42754132/article/details/100063782

3.9 STL containers opportunity

 

vector

and

list

set

multiset

map

multimap

Typical structure Memory

Single-ended array

An array of double-ended

Doubly linked list

Binary Tree

Binary Tree

Binary Tree

Binary Tree

Random access

Yes

Yes

no

no

no

For the key: not

no

Search speed element

slow

slow

very slow

fast

fast

For the key: fast

For the key: fast

Remove the element placement

The end of

Head and tail ends

any position

-

-

-

-

 

 

  1. vector usage scenarios: for example, the operation history record storage software, we often want to view the history, such as the last record, the last record, but not to delete records because the records are described facts.
  2. deque usage scenarios: ticketing system such as queuing, storing line up deque may be employed, to support rapid removal of the head end, quickly add the trailing end. If Vector, when the tip is removed, will move large amounts of data, slow.

     vector and deque comparison:

      A: vector.at () () higher than deque.at efficiency, such vector.at (0) is fixed, the start position of the deque is not fixed.

    Two: If you have a large number of operations, then released, vector spend less time with which both internal implementation dependent.

    Three: deque supports rapid insertion and rapid removal of the head, which is an advantage of the deque.

  1. list of usage scenarios: for example, the bus passengers storage, at any time there may be passengers to get off, do not support the removal of the frequent insertion position of the element.
  2. set to be used: for example storage, storage requirements of the mobile game scoring record personal sequentially arranged from high to low score. 
  3. map usage scenarios: for example, by ID number stored thousands of users, you want to quickly find the corresponding user by ID. Find the efficiency of a binary tree, then it manifested. If the container is a vector, in the worst case possible to traverse the full container to locate the user.

4. Common algorithm

4.1 function object

Overload class function call operator, which is often referred to as the object function object (function object), i.e., they are similar to the behavior of the object function, also called functor (functor), is actually overloaded "()" operator, such that class object like a function call as possible.

note:

1. function object (functor) is a class, not a function.

2. A function object (functor) overrides "()" operator so that it can be called like a function.

Classification: Suppose that a class has an overloaded operator (), and overloaded operator () Gets a required parameter, we will call this class " one yuan functor" (unary functor) ; the contrary, if the overloaded operator () takes two parameters required, this will be referred to as class ' binary functor "(binary functor) .

What is the role of the main function object? STL provides algorithms tend to have two versions, one of which showed the most commonly used version of a certain operation, another version allows the user to specify the policy to be taken in the form of template parameters.

 

// function object class is overloaded function call sign

class MyPrint

{

public:

    MyPrint()

    {

       m_Num = 0;

    }

    int m_Num ;

 

public:

    void operator() (int num)

    {

       cout << num << endl;

       m_Num ++;

    }

};

 

// function object

// overloaded () class instantiated objects operator, like ordinary function calls that can , can have parameters , there may be a return value

void test01 ()

{

    MyPrint myPrint;

    myPrint(20);

 

}

// function object beyond the general concept of a function, you can save state function call

void test02 ()

{

    MyPrint myPrint;

    myPrint(20);

    myPrint(20);

    myPrint(20);

    cout << myPrint.m_Num << endl;

}

 

void doBusiness(MyPrint print,int num)

{

    print ( whether );

}

 

// function object as a parameter

void test03()

{

    //参数1:匿名函数对象

    doBusiness(MyPrint(),30);

}

 

总结:

1、函数对象通常不定义构造函数和析构函数,所以在构造和析构时不会发生任何问题,避免了函数调用的运行时问题。

2、函数对象超出普通函数的概念,函数对象可以有自己的状态

3、函数对象可内联编译,性能好。用函数指针几乎不可能

4、模版函数对象使函数对象具有通用性,这也是它的优势之一


11 仿函数 函数对象(仿函数)
  重载() 所以函数的对象 使用()像函数调用
  是类 而不是普通的函数
  内部记录状态
  作为类型 与模板进行配合

#include <iostream>
using namespace std;
class MyPrint{

public:
    void operator()(int num){
        cout<<"num="<<num<<endl;

        count++;//统计调用了多少次仿函数
    }
int count=0;
};


void MyPrint2(int num){
        cout<<"num="<<num<<endl;
}

void test01(){
    //MyPrint是一个类 而不是函数
    MyPrint myprint;
    myprint(111);//看起来像函数调用,仿的形式上的东西
    //MyPrint2(222);//实际的函数

    MyPrint()(1000);//匿名对象的形式调用

}

//函数对象已经超出了普通函数的概念,内部可以保存状态
//有点像其他语言的闭包
void test02(){
    MyPrint myprint;
     myprint(111);
     myprint(111);
     myprint(111);
     myprint(111);
     myprint(111);

     cout<<"myprint使用次数:"<<myprint.count<<endl;

//一个类中可以用仿函数记录属性


}

//函数对象作为参数
void doPrint(MyPrint print,int num){
    print(num);
}

void test03(){
   doPrint(MyPrint(),20);

   // set<int,MyPrint>s;

}

int main(){
  //  test01();
 // test02();
    test03();
    return 0;
}

 

4.2 谓词

谓词是指普通函数重载的operator()返回值是bool类型的函数对象(仿函数)。如果operator接受一个参数,那么叫做一元谓词,如果接受两个参数,那么叫做二元谓词,谓词可作为一个判断式。

class GreaterThenFive

{

public:

    bool operator()(int num)

    {

       return num > 5;

    }

 

};

//一元谓词

void test01()

{

    vector<int> v;

    for (int i = 0; i < 10;i ++)

    {

       v.push_back(i);

    }

   

     vector<int>::iterator itfind_if(v.begin(), v.end(), GreaterThenFive());

     if (it == v.end())

     {

        cout << "没有找到" << endl;

     }

     else

     {

        cout << "找到了: " << *it << endl;

     }

}

 

//二元谓词

class MyCompare

{

public:

    bool operator()(int num1, int num2)

    {

       return num1 > num2;

    }

};

 

void test02()

{

    vector<int> v;

    v.push_back(10);

    v.push_back(40);

    v.push_back(20);

    v.push_back(90);

    v.push_back(60);

 

    //默认从小到大

    sort(v.begin(), v.end());

    for (vector<int>::iterator it = v.begin(); it != v.end();it++)

    {

       cout << *it << " ";

    }

    cout << endl;

    cout << "----------------------------" << endl;

    //使用函数对象改变算法策略,排序从大到小

    sort(v.begin(), v.end(),MyCompare());

    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)

    {

       cout << *it << " ";

    }

    cout << endl;

}


 12 谓词
 重载() 返回值是bool类型
 普通函数或者仿函数 返回值为bool类型
 一元 一个参数 二元 两个参数
 一元查找大于20的数字 find_if 返回迭代器
 二元排序 借助lambda 或回调函数
 

 

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class GreaterThan20{
public:
    bool operator()(int value){
        return value>20;
    }
};




//一元谓词
//一元体现在在有一个参数 谓词体现在 返回值为bool类型

void test01(){
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);
    v.push_back(60);

    //查找第一个大于20的数字
   // GreaterThan20 greaterThan;
  //  find_if(v.begin(),v.end(),greaterThan);//第三个参数是函数对象 匿名对象可以
  vector<int>::iterator pos=find_if(v.begin(),v.end(), GreaterThan20());//返回一个迭代器 找不到返回end位置
  if(pos!=v.end()){
      cout<<"找到大于20的数"<<*pos<<endl;
  }else{
      cout<<"未找到"<<endl;
  }


}


class MyCompare{
public:
    bool operator()(int v1,int v2){
        return v1>v2;
    }
};

bool MYCOM(int v1, int v2){
   return v1>v2;
}


//二元谓词
void test02(){
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);
    v.push_back(60);
   // sort(v.begin(),v.end(),MYCOM);
    sort(v.begin(),v.end(),MyCompare());
    for_each(v.begin(),v.end(),[](int value){ cout<<value<<endl;});//也可以写一个回调函数
    //匿名函数 lambda表达式 [](){}..[]标识符.():参数列表.{}函数体
}




int main(){
    //test01();
test02();
    return 0;
}

 

 

(本笔记内容整理自网络资源,侵删)

 

 

 

Guess you like

Origin blog.csdn.net/qq_42754132/article/details/100063782