zoukankan      html  css  js  c++  java
  • 10.ref regex unordered_set smartpoint

    • ref引用不可以复制的对象
       1 void print(std::ostream &os, int i)
       2 {
       3     os << i << endl;
       4 }
       5 
       6 //ref引用不可以复制的对象
       7 void main1()
       8 {
       9     vector<int> myint = { 1,2,3,4,5 };
      10     //cout是不可以拷贝的对象,以下出错
      11     //boost::function<void(int)> pt = boost::bind(print, cout, _1);
      12     //ref可以引用不可以复制的对象
      13     boost::function<void(int)> pt = boost::bind(print,boost::ref(cout), _1);
      14     for_each(myint.begin(), myint.end(), pt);
      15     cin.get();
      16 }
    • 底层是基于hasn的unordered_set集合
       1 //底层是基于hasn的unordered_set集合
       2 void main5()
       3 {
       4     boost::unordered_set<string> myhash;
       5     myhash.insert("1234");
       6     myhash.insert("1234a");
       7     myhash.insert("1234b");
       8     for (auto ib = myhash.begin(), ie = myhash.end(); ib != ie; ib++)
       9     {
      10         cout << *ib << endl;
      11     }
      12     cin.get();
      13 }
    • 独享智能指针
       1 void main6()
       2 {
       3     boost::scoped_ptr<int> p1(new int(5));//智能指针
       4     boost::scoped_ptr<int> p2(new int[5]);//智能指针,可以分配但是不推荐
       5     //不可以这样,因为是独享内存
       6     //boost::scoped_ptr<int> p3 = p2;
       7     //释放
       8     p2.reset();
       9     //重新分配内存
      10     p2.reset(new int(3));
      11     //获取第一个
      12     cout << *p2.get() << endl;
      13 
      14     //智能指针,存储数组
      15     boost::scoped_array<int> p(new int[5]);
      16     p[3] = 2;
      17     *p.get() = 1;
      18     //释放内存
      19     p.reset();
      20     //重新分配内存
      21     p.reset(new int[4]);
      22     cin.get();
      23 }
    • 共享智能指针
       1 class AobjC
       2 {
       3 public:
       4     AobjC()
       5     {
       6         cout << "构造" << endl;
       7     }
       8     ~AobjC()
       9     {
      10         cout << "析构" << endl;
      11     }
      12 protected:
      13 private:
      14 
      15 };
      16 
      17 void main7()
      18 {
      19     //共享内存
      20     boost::shared_ptr<AobjC> p1(new AobjC);
      21     boost::shared_ptr<AobjC> p2(new AobjC);
      22     //浅拷贝
      23     boost::shared_ptr<AobjC> p3(p2);
      24     p3.reset();
      25     p3.reset(new AobjC);
      26     //计数
      27     p3.use_count();
      28 
      29     boost::shared_array<AobjC> p4(new AobjC[4]);
      30     cin.get();
      31 }
    • 多线程不能用shared_ptr,要用weak_ptr
       1 //shared智能指针,多线程不安全
       2 //DWORD WINAPI printout(LPVOID p)
       3 //{
       4 //    boost::shared_ptr<int> *pint = static_cast<boost::shared_ptr<int>*>(p);
       5 //    for (int i = 0; i < 5; i++)
       6 //    {
       7 //        cout << **pint << endl;
       8 //        Sleep(1000);
       9 //    }
      10 //    return 0;
      11 //}
      12 //
      13 //DWORD WINAPI reset(LPVOID p)
      14 //{
      15 //    boost::shared_ptr<int>*pint = static_cast<boost::shared_ptr<int>*>(p);
      16 //    //释放内存
      17 //    pint->reset();
      18 //    return 0;
      19 //}
      20 
      21 DWORD WINAPI printout(LPVOID p)
      22 {
      23     //weak_ptr接管指针
      24     //传递的是int指针的地址,所以要用boost::weak_ptr<int> *pw(多一个*保存)
         //传递地址要多加一个*(指针要多一级,便于多一次读取,第一次读存储的地址,第二次读内容)
      25 boost::weak_ptr<int> *pw = static_cast<boost::weak_ptr<int>*>(p); 26 //弱指针可以锁定,防止多线程访问一个被释放的内存 27 boost::shared_ptr<int> newp = pw->lock(); 28 for (int i = 0; i < 5; i++) 29 { 30 if (newp) 31 { 32 cout << *newp << endl; 33 } 34 else 35 { 36 cout << "内存释放无法访问" << endl; 37 } 38 Sleep(3000); 39 } 40 return 0; 41 } 42 43 DWORD WINAPI reset(LPVOID p) 44 { 45 Sleep(1); 46 boost::shared_ptr<int>*pint = static_cast<boost::shared_ptr<int>*>(p); 47 //释放内存 48 pint->reset(); 49 return 0; 50 } 51 52 void main() 53 { 54 boost::shared_ptr<int> p1(new int(10)); 55 //帮助在多线程中实现内存保护,没有重载*运算符 56 boost::weak_ptr<int> p2 = p1; 57 HANDLE threads[2]; 58 threads[0] = CreateThread(0, 0, reset, &p1, 0, 0); 59 threads[1] = CreateThread(0, 0, printout, &p2, 0, 0); 60 //C++11也有weak_ptr 61 //std::weak_ptr; 62 cin.get(); 63 }
    • 正则表达式
       1 //正则表达式
       2 void main2()
       3 {
       4     string str = "china8848english";
       5     boost::regex expr("\w+\d+\w+");//w+字符 d+数字
       6     cout << boost::regex_match(str, expr);
       7     cin.get();
       8 }
       9 
      10 //找出匹配的
      11 void main3()
      12 {
      13     string str = "china8engli5sh";
      14     boost::regex expr("(\w+)\d(\w+)");
      15     //保存匹配的
      16     boost::smatch what;
      17     if (boost::regex_search(str, what, expr))
      18     {
      19         cout << what[0] << endl;
      20         cout << what[1] << endl;
      21     }
      22     else
      23     {
      24         cout << "fail";
      25     }
      26 }
      27 
      28 //替换
      29 void main4()
      30 {
      31     string str = "china8engli5sh";
      32     boost::regex expr("(\w + )\d(\w + )");
      33     string rp = "--------";
      34     cout << boost::regex_replace(str, expr, rp);
      35 }
  • 相关阅读:
    Spring有用功能--Profile、WebService、缓存、消息、ORM
    opencv标定程序(改动)
    Install Docker Mac OS X
    Android eclipse 提示java代码 快捷键
    Mac使用Docker-machine訪问docker publish port
    决策树之C4.5算法学习
    为ImageView设置背景图片(代码中)
    BZOJ 3675 APIO2014 序列切割 斜率优化DP
    思科模拟器之路由器-RIP-DNS解析server
    POJ 3177 Redundant Paths
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8654780.html
Copyright © 2011-2022 走看看