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.

  • 相关阅读:
    python学习-基础-day5-文件操作和函数
    (转) 什么是立即执行函数?有什么作用?
    AngularJS 从DOM中获取scope
    AngularJS中Directive指令系列
    AngularJS中Directive指令系列
    angularJS ng-repeat中的directive 动态加载template
    (转) 前端面试之js相关问题(一)
    ES6 localStorage 类库
    underscore.js 源码分析5 基础函数和each函数的使用
    记录使用jQuery和Python抓取采集数据的一个实例
  • 原文地址:https://www.cnblogs.com/tybcode/p/5935362.html
Copyright © 2011-2022 走看看