STL的相关知识1

function:

函数对象,也是一个模板
语法:
function<void (param…)>

#include<iostream>
using namespace std;
void add(int a,int b){
    return a+b;
}
struct functor{
    int operator()(int a,int b){
        return a+b;
    }
    int add(int a,int b){
        return a+b;
    }
    static  int ADD(int a,int b){
        return a+b;
    }
    int value;
    static int s_value;
};
int functor::s_value=1000;
int main(){
   function<int(int,int)>f1=&add;
   cout<<"普通函数"<<f1(123,456)<<endl;
   function<int(int,int)>f2=functor();
   cout<<"仿函数"<<f2(123,456)<<endl;
   functor obj;
   function<int(functor*,int,int)>f3=&functor::add;
   cout<<"class member function ="<<f3(&obj,123,456)<<endl;
   function<int(int,int)>f4=&functor::ADD;
   cout<<"class static function ="<<f4(123,456)<<endl;
   function<int(int,int)>f5=[](int a,int b){
       return a+b;
   };
    cout<<"lambda function ="<<f5(123,456)<<endl;
    function<int(functor*)>f6=&functor::value;
    cout<<f6(&obj)<<endl;
    //function<int()>f7=&functor::s_value;//无法用 class static porperty 初始化
}

bind:

可以用bind实现我们的一个闭包
bind(func_name,arg1,arg2…);
bind(func_name,std::placeholders::_1,std::placeholders::__2…);
可以将函数转换成新函数对象
1.转换形参的作用顺序,2就是对形参可以做一些修改(增减参数)
3.参数绑定到一个闭包对象中,最后通过function来调用

特殊绑定:
将二元函数对象(module,less等)转换为一元函数对象
1.bind1st()
2.bind2nd()

#include<iostream>
#include<functional>
using namespace std;
int add(int a,int b){
    cout<<"a="<<a<<"b="<<b<<endl;
    return a+b;
}
struct functor{
    functor(){}
    int operator(int a,int b){
        return a+b;
    }
     int add(int a,int b){
        return a+b;
    }
};
void testRef(int&n){
    n++;
}
int main(){
    //2个参数转为0个参数
    function<int()> f1= bind(add,1,2);
    cout<<f1()<<endl;
    //2个参数转为1个参数
     function<int(int)> f2= bind(add,123,std::placeholders::_1);
    cout<<f2(456)<<endl;
    //排列参数顺序
    function<int(int,int)> f3= bind(add,std::placeholders::_2,std::placeholders::_1);
    cout<<f3(123,456)<<endl;
    function<int(int,int)> f4= bind(add,std::placeholders::_1,std::placeholders::_2);//最多调用25个
    cout<<f4(123,456)<<endl;
    functor f;
    function<int(int,int)>f5=bind(&functor::add,&f,std::placeholders::_1,std::placeholders::_2);
    cout<<f5(123,456)<<endl;
    function<int(int,int)>f6=bind(&functor::operator(),&f,std::placeholders::_1,std::placeholders::_2);
    cout<<f6(123,456)<<endl;
    function<int(int,int)>f7=bind(f,std::placeholders::_1,std::placeholders::_2);
    cout<<f7(123,456)<<endl;
    int m=1;
    function<void()>func=bind(testRef,std::ref(m));
    func();
    func();
    cout<<m<<endl;
    int a[]={1,2,3,4,5,6,7,8};
    // int count=count_if(a,a+9,[](int n){return n%2;});
    int count=count_if(a,a+9,bind2nd(modulus<int>(),2));
    //用modules求奇数个数
    cout<<count<<endl;
    cout<<">3= "<<count_if(a,a+9,bind1st(less<int>(),3))<<endl;
    cout<<"<3="<<count_if(a,a+9,bind2nd(less<int>(),3))<<endl;
    return 0;
    //less_equal
    //greater
    //greater_equal
    //not1
    //not2
}

智能指针:

auto_ptr:c++11后不使用
1.unique_ptr:维护唯一的指针,不可复制
2.shared_ptr:可以传递,复制 (调用operator=()),引用计数
3.weak_ptr:弱引用(随时可能失效),指向一个shared_ptr,lock()
内存开辟,内存释放,指针赋值(转移)

//uique_ptr
#include<iostream>
#include<memory>
using namespace std;
struct A{
    A(){
        cout<<"constructor"<<this<<endl;
    }
    A(int a):x(a){
        cout<<"constructor int "<<this<<endl;
    }
    ~A(){
         cout<<"deconstructor"<<this<<endl;
    }
    int x;
}
int main(){
      unique_ptr<A>p1;
      unique_ptr<A>p2=make_unique<A>(123);
      unique_ptr<A>p3(new A());
      if(p1)
      cout<<p1->x<<endl;
      cout<<p2->x<<endl;
      A*p=p3.get();
      p->x;
      cout<<"----------\n";
      //p2.reset();//delete point and set nullptr
      A*point=p2.release();//不会释放内存,set nullptr
      //p1=p;
      //p1=std::move(p2);
      p1.swap(p2);
      cout<<"p1="<<p1.get()<<"p2 ="<<p2.get()<<endl;
      delete point;
      cout<<"----------\n";
    return 0;
}
//shared_ptr
#include<iostream>
#include<memory>
using namespace std;
struct A{
    A(){
        cout<<"constructor"<<this<<endl;
    }
    A(int a):x(a){
        cout<<"constructor int "<<this<<endl;
    }
    ~A(){
         cout<<"deconstructor"<<this<<endl;
    }
    int x;
}
int main(){
     shared_ptr<A>p1;
     shared_ptr<A>p2=make_shared<A>(123);
     shared_ptr<A>p3=(new A());
    //  p1.use_count();//引用计数
    //  p1.unique();
     p1=p2;
     shared_ptr<A>p4=p2;
     cout<<"p1 count = "<<p1.use_count()<<" unique = "<<p1.unique()<<p1.get()<<endl;
      cout<<"p2 count = "<<p2.use_count()<<" unique = "<<p2.unique()<<p2.get()<<endl;
       cout<<"p3 count = "<<p3.use_count()<<" unique = "<<p3.unique()<<p3.get()<<endl;
       cout<<p2.get()->x<<" "<<p2->x<<endl;
       if(p1){
           cout<<p1->x<<endl;
       }else{
           cout<<"p1 is nullptr"<<endl;
       }
       cout<<"-----------\n"
       p1.reset();
       p2.reset();
       p4.reset();
       cout<<"-----------\n"
    return 0;
}
实现shared_ptr
#include<iostream>
#include<memory>
namespace master{
template<class T>
class shared_ptr{
public:
shared_ptr():_ptr(nullptr),_ref(new int{0}){

}
shared_ptr(T* t):_ptr(t),_ref(new int{1}){

}
shared_ptr(const shared_ptr<T>& other):_ptr(other._ptr),ref(other._2ref){
 (*_ref)++;
}
shared_ptr(shared_ptr<T>&&other):_ptr(nullptr),_ref(new int{0}){
   std::swap(_ptr,other._ptr);
    std::swap(_ref,other._ref);
    other.reset();
}
virtual ~shared_ptr(){
    if(_ptr){
        (*_ref)--;
        if((*ref)==0){
            delete _ptr;
            delete _ref;
        }
    }
}
void  reset(T*t=nullptr){
    if(_ptr){
    (*_ret)--;
    if((*_ref)==0){
        delete _ptr;
        _ptr=nullptr;
    }else{
        _ref=new int{0};
        _ptr=nullptr;
    }
  }
  if(t){
      _ptr=t;
      (*_ref)++;
  }
    
}
void swap(shared_ptr<T>&other){
    std::swap(_ptr,other._ptr);
    std::swap(_ref,other._ref);
}
void swap(shared_ptr<T>&&other){
    this->reset();
    if(_ref) delete _ref;
    _ptr=other._ptr;
    _ref=other._ref;
    other._ptr=nullptr;
    other._ref=nullptr;
}
shared_ptr<T>&operator=(const shared_ptr<T>&other){
    this->reset();
    if(_ref) delete _ref;
    _ptr=other._ptr;
    _ref=other._ref;
    (*_ref)++;
    return *this;
}
shared_ptr<T>&operator=( shared_ptr<T>&&other){
    this->swap(std::forward<shared_ptr<T>&&>(other));
    return *this;
}
operator bool(){
    return _ptr ? true:false;
}
T*operator->(){
    return _ptr;
}
T& operator*(){
    return *_ptr;
}
inline bool unique(){  return (*_ref)==1?true:false;}
inline int use_count(){  return (*_ref);}
inline T*get(){  return _ptr;}
use_count();
get()
private:
       T*ptr;
       int*_ref;
    };
}
int main(){
     //weak_ptr必须和share_ptr组合使用
     std::shared_ptr<int>p1{new int{1}};
     std::shared_ptr<int>p2(p1);
     
     //sp.reset();
     
     p1.reset();
     p1.swap(p2);
     p1.unique();
     p1.use_count();
     p1.get();
     
}

//weak_ptr
#include<iostream>
#include<memory>
using namespace std;
struct A{
    A(){
        cout<<"constructor"<<this<<endl;
    }
    A(int a):x(a){
        cout<<"constructor int "<<this<<endl;
    }
    ~A(){
         cout<<"deconstructor"<<this<<endl;
    }
    int x;
}
int main(){
     //weak_ptr必须和share_ptr组合使用
     shared_ptr<A>sp=make_shared<A>(123);
     weak_ptr<A>p1(sp);
     //sp.reset();
     {
     p1.reset()
     shared_ptr<A> p=p1.lock();
     if(p){//正确使用weak_ptr的方式
       cout<<"p locked="<<p<<endl;
        cout<<p->x<<endl;
     }else{
         cout<<"p locked="<<p<<endl; 
     }   
     cout<<p1.expired()<<endl;
     cout<<p1.use_count()<<endl;//引用计数//2
     }
      cout<<"after lock"<<p1.use_count()<<endl;//引用计数//1
    return 0;
}

thread:

#include<iostream>
#include<functional>
#include<thread>
#include<mutex>
#include<atomic>
using namespace std;
mutex mtx;
void print(int n){
    
    for(int i=0;i<=n;i++){
            mtx.lock();
            cout<<i<<endl;
            mtx.unlock();
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
    
}
void myadd(int &n){
    for(int i=1;i<=10;i++){
        mtx.lock();
        n++;
        mtx.unlock();
        cout<<n<<endl;
         std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}
void myadd2(atomic<int>&n){//原子操作比加锁的性能更高,这是一个模板类(int,unsigned int ,long,)不是任何地方都可使用原子操作
    for(int i=1;i<=10;i++){
        n++;
        cout<<n<<endl;
         std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}
int main(){
   int m=0;
   thread t1(myadd,ref(m));
   thread t2(myadd,ref(m));
   auto t=clock();
   t1.join();//把t1线程加入到当前线程中,此时主线程处于阻塞状态
   t2.join();
   cout<<m<<endl;
   cout<<"====="<<endl;
     std::this_thread::sleep_for(std::chrono::milliseconds(3000));
     t1.detach();
     t2.detach();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值