zoukankan      html  css  js  c++  java
  • c++ tips

    c++

    - std::include //if subset
      std::include(collection1.begin(), collection1.end(), collection2.begin(), collection2.end()} => if collection2 is a subset of collection1 //vector, set

    - std::map, multimap, set, multiset
      Implemented using a red-black tree(a type of balanced binary search tree) with O(logn) Insert/Lookup/Deletion
      std::multiset has all elements heap-sorted, m.insert(n), m.erase(n)//delete all values=n, m.find(n), m.rbegin()=pointer to max val in m

    // erasing from multiset
    #include <iostream>
    #include <set>
    
    int main ()
    {
      std::multiset<int> mymultiset;
      std::multiset<int>::iterator it;
    
      // insert some values:
      mymultiset.insert (40);                            // 40
      for (int i=1; i<7; i++) mymultiset.insert(i*10);   // 10 20 30 40 40 50 60
    
      it=mymultiset.begin();
      it++;                                              //    ^
    
      mymultiset.erase (it);                             // 10 30 40 40 50 60
    
      mymultiset.erase (40);                             // 10 30 50 60
    
      it=mymultiset.find (50);
      mymultiset.erase ( it, mymultiset.end() );         // 10 30
    
      std::cout << "mymultiset contains:";
      for (it=mymultiset.begin(); it!=mymultiset.end(); ++it)
        std::cout << ' ' << *it;
      std::cout << '
    ';
    
      return 0;
    }

    - std::hash_map, hash_set, hash_multimap, hash_multiset
      Implemented using hash_tables() with O(1) expected, O(n) worst case Insert/Lookup/Deletion

    - custom comparator
      1. inline
      struct my_class { int x,y,z; my_class(int x_val, int y_val, int z_val):x(x_val),y(y_val),z(z_val){} };
      vector<my_class> my_objs;
      sort(my_objs.begin(), my_objs.end()), [](const my_class &a, const my_class &b) {
          if(a.x != b.x) return a.x < b.x;
          else if(a.y != b.y) return a.y < b.y;
          else return a.z < a.z;
      });
      2. overload operator <
      struct my_class {
           int x,y,z;
           my_class(int x_val, int y_val, int z_val):x(x_val),y(y_val),z(z_val){}
           bool operator < (const my_class &other) {
                 if(a.x != b.x) return a.x < b.x;
                 else if(a.y != b.y) return a.y < b.y;
                 else return a.z < a.z;
          });
     };
     vector<my_class> my_objs;
     sort(my_objs.begin(), my_objs.end()); //apply overloaded operator <

    ////////////////////////
    English Expression

    - A rather straight forward solution is a ... algorithm using ... (First... , then ...) We can come up with a ... algorithm using only ...
      A rarther straight forward solution is a two-pass algorithm using counting sort.
      First i/'i/terate the array counting number of 0's, 1's and 2's, then overwrite the array with total number of 0's, then 1's and followed by 2's.
      This solution requires to iterate the array twice, and uses only constant space, i.e. 3 counters
      We can come up with an one-pass algorithm using only constant space.

  • 相关阅读:
    重构之重新组织函数(ExTract Method)
    设计模式之桥接模式
    设计模式原则之里氏替换原则
    设计模式原则之依赖倒置原则
    设计模式原则之接口隔离原则
    设计模式原则之单一职责原则
    编译php5.6
    wireshark总结
    Blu-Ray BRRip 和 BDRip 的区别
    openwrt虚拟机的network unreachable
  • 原文地址:https://www.cnblogs.com/tybcode/p/5935362.html
Copyright © 2011-2022 走看看