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.

  • 相关阅读:
    Spark&Hive结合起来
    spark&dataframe
    JAVA-数组或集合
    九种经典排序算法详解(冒泡排序,插入排序,选择排序,快速排序,归并排序,堆排序,计数排序,桶排序,基数排序)
    B+树介绍
    @transactional注解在什么情况下会失效,为什么。
    一个ArrayList在循环过程中删除,会不会出问题,为什么?
    Java:传值还是传引用?
    Java序列化的方式。
    实现动态代理的两种方式
  • 原文地址:https://www.cnblogs.com/tybcode/p/5935362.html
Copyright © 2011-2022 走看看