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.

  • 相关阅读:
    IDEA使用总结1-Github下载代码和上传代码到Git
    Mysql-安装指南
    Dubbo安装及其实战1
    分布式设计(学习内容目录--后端架构师)
    elasticsearch安装指导(new)
    浅谈TCP/IP(new 常见面试问题)
    浅谈常用的设计模式(new)
    浅谈Elasicsearch
    浅谈数据库分库分表(new)
    JAVA 电子书下载地址
  • 原文地址:https://www.cnblogs.com/tybcode/p/5935362.html
Copyright © 2011-2022 走看看