/images/art2.png

Bit operation in C++

Bit operator operator function example « left shift 0001 –> 0010 » right shift 0010 –> 0001 & and (bit by bit) 1100 & 1010 = 1000 | or (bit by bit) 1010 | 0101 = 1111 ~ reverse ~0000 = 1111 ^ XOR 0110 ^ 1100 = 1010 Operator: & x is a bit x & 1 = x x & 0 = 0 Usually use & as a filter.

RB tree

2-3 tree & 2-4 tree 2-node: 1 key and 2 children 3-node: 2 keys and 3 children 4-node: 3 keys and 4 children A (2,4) tree (also called 2-4 tree or 2-3-4 tree) is a multi-way search with the following properties: Node-Size Property: every internal node has at most four children Depth Property: all the external nodes have the same depth insertion deletion 2-4 tree ==> RB tree If break down 3-node and 4-node, 2-4 tree will become RB tree.

Operator overwrite in c++

Example increment and decrement operator 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 #include <iostream> using namespace std; class INT { private: int m_i; public: INT(int i):m_i(i){}; friend bool operator==(INT& test1, INT& test2){ return test1.

Container in c++

Classification Sequence container array (build in) vector heap priority queue list slist (not standard) deque stack (adopter) queue (adopter) Associative container RB-tree (not public) set map multiset multemap hashtable (not standard) hash_set (not standard) hash_map (not standard) hash_multimap (not standard) hash_multiset (not standard) Associative container have a key-value pair. It do not have back and front so they never have push_back, pop_back. Vector This is similar with grow array. Vector use sequential space.

explicit and implicit in c++

explicit and implicit In C++, constructor can be explicit and implicit. The reserve word explicit affect constructor with only one parameter or only one parameter without given initial value. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 #include <iostream> using namespace std; class test1 { int data; public: test1(int t = 0):data(t){}; ~test1(){}; test1 operator + (const test1 &a) const{ return test1(data + a.

Iterator in c++

Iterator 设计思维 STL 中 container 和 algorithm 是相对独立的,本身设计也是泛型化的。Iterator 就是用来将这两者联系在一起的。 Iterator 是一种 smart pointer 可以不用 delete Iterator 属性 iterator_traits 是用来抽取 iterator 中的类型(特指 value type)的。 这是 iterator 中常见的五种属性。 1 2 3 4 5 6 7 8 9 template <class I> struct iterator_traits { typedef typename I::iterator_category iterator_category; //category typedef typename I::value_type value_type; // type typedef typename I::difference_type difference_type; // typedef typename I::pointer pointer; // T* typedef typename I::reference reference; // T& }; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <iostream> // std::cout #include <iterator> // std::iterator_traits #include <typeinfo> // typeid using namespace std; int main() { typedef std::iterator_traits<double*> traits; cout << typeid(traits::iterator_category).

Allocator in c++

Allocator is for memory 配置内存空间 -> 构建(constructor) -> 解构(destructor) -> 释放内存空间 construct() and destroy() 用于建构和解构 Memory allocate and release 双层配置器。第一级是区块大于 128 bytes 的,使用 malloc()和 free()。第二级是区块小于 128 bytes 的,使用 memory pool 和 freelist。 第一級配置器 第一級配置器以 malloc(), free(), realloc() 等 C 函式執行實際的記憶體配置、釋放、重配置動作,並實作出類似 C++ new-handler7 機制。是的,它不能直接運用 C++ new-handler 機制,因為它並非使用 ::operator new 來配置記 憶體。 所謂 C++ new handler 機制是,你可以要求系統在記憶體配置需求無法被滿足時, 喚起一個你所指定的函式。換句話說一旦 ::operator new 無法達成任務,在丟出 std::bad_alloc 異常狀態之前,會先呼叫由客端指定的處理常式。此處理常式 通常即被稱為 new-handler。new-handler 解決記憶體不足的作法有特定的模式。 注意,SGI 以 malloc 而非 ::operator new 來配置記憶體(我所能夠想像的一 個原因是歷史因素,另一個原因是 C++ 並未提供相應於 realloc() 的記憶體配 置動作),因此 SGI 不能直接使用 C++ 的 set_new_handler(),必須模擬一個 類似的 set_malloc_handler()。

Functor in C++

Basically, Functor have same functionality with interface in java. Think about this code. You want count all string whose length is less than 5. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; bool lenLessThanFive(const string& str){ return str.size() < 5; } int main(int argc, char const *argv[]) { string ia[5] = {"a", "aa", "aaa", "aaaa", "aaaaa"}; vector<string> iv(ia, ia+5); int res = count_if(iv.

Common method comparison - c++ & java

Sort C++ For c++, here is official document sort() This method sorts elements in the range [first, last). Result is in ascending order by deflaut. Introsort. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 // sort algorithm example #include <iostream> // std::cout #include <algorithm> // std::sort #include <vector> // std::vector bool myfunction (int i,int j) { return (i>j); } int main () { int myints[] = {32,71,12,45,26,80,53,33}; std::vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33 // using default comparison (operator <): std::sort (myvector.

Container comparison - c++ & java

Container comparison C++ version is c++11. Java version is java se8. C++ JAVA Description array / [ ] [ ] 固定大小的数组 vector ArrayList 可变长度的数组 Vector 可变长度的数组,支持同步操作,效率比 ArrayList 略差 list LinkedList 双链表,便于增删 forward_list 单链表,c++没有给他提供 size()的方法 deque ArrayDeque 双向队列 stack Stack 栈,先进后出 queue Queue 队,先进先出 priority_queue PriorityQueue 支持优先级的队列 set TreeSet 集合,数据有序,红黑树 unordered_set HashSet 集合,数据无序,hash map TreeMap key-value 映射,key 有序,红黑树 unordered_map HashMap map, 无序,hash multiset 集合,允许重复元素 multimap map,允许重复的 key unordered_multiset 无序允许重复元素集合 unordered_multimap 无序允许重复 key 的 map LinkedHashSet 按照插入顺序,支持 hash 查找 LinkedHashMap 按照插入顺序,支持 hash 查找 HashTable 类似 HashMap,效率略低 bitset BitSet 位操作 HashTable & HashMap The HashMap class is roughly equivalent to Hashtable, except that it is asynchronized and permits nulls.