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.

  • 相关阅读:
    在C#中,不安装Oracle客户端如何连接Oracle数据库
    敏捷宣言(四) 猪和鸡的故事
    敏捷宣言(六) 单单有敏捷就够了吗?
    敏捷宣言(五) 看板是另外一种敏捷实践
    敏捷宣言(七) 软件系统
    小白知识摘录__进程和线程
    Linux系统修改/etc/sysconfig/i18n文件,桌面无法正常显示
    小白知识摘录__环境变量
    hive表查询中文显示乱码
    3月10日晚
  • 原文地址:https://www.cnblogs.com/tybcode/p/5935362.html
Copyright © 2011-2022 走看看