Monday, February 11, 2019

C++ Data Structure

Data Type Limits (limits.h)
#include <climits>

int main()
{
    printf ( "char_min %d, char_max %d, int_min %d, int_max %d\n", 
        CHAR_MIN, CHAR_MAX, INT_MIN, INT_MAX );
}
C++ Arrays
#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
    int * ia = new int[5];
    int ib[5];
    int *ip = ib;
    ib[0] = 1;
    *ib = 1;
    *ip = 2;
    ++ip;
    *ip = 3;
    *(++ip) = 4;
    int ic [] = { 1,2,3,4,5 };
    int id [5] = { 1,2,3,4,5 };

    // cstring, a primitive string
    char s0[] = "string";
    char s[] = { 's', 't', 'r', 'i', 'n', 'g', 0 };
    for (int i = 0; s[i] != 0; ++i ) {
        printf ( "%c ", s[i] );
    }
    cout << endl;
    for (char * cp = s; *cp; ++cp ) {
        printf ( "%c ", *cp );
    }
    cout << endl;
    // range based loop
    for (char c : s ) {
        printf ( "%c ", c );
    }
    cout << endl;
}
C++ Containers
Sequence containers:
- array                              - Fixed size, Direct access to any element.
- deque                            - Rapid insertions and deletions at front or back.
- forward_list                 - Singly linked list, rapid insertion and deletion anywhere. New in C++11
- list                                  - Doubly linked list, rapid insertion and deletion anywhere.
- vector<T>                     - Rapid insertions and deletions at back. Direct access.

Ordered associative containers - keys are maintained in sorted order.
- set                                  - Rapid lookup, no duplicates allowed.
- multiset                         - Rapid lookup, duplicates allowed.
- map                                - one-to-one mapping, no duplicates allowed. Rapid key-based lookup.
- multimap                      - one-to-one mapping, duplicates allowed. Rapid key-based lookup.

Un-ordered associative containers
- unordered_set              - Rapid lookup, no duplicates allowed.
- unordered_multiset     - Rapid lookup, duplicates allowed.
- unordered_map            - one-to-one mapping, no duplicates allowed. Rapid key-based lookup.
- unordered_multimap   - one-to-one mapping, no duplicates allowed. Rapid key-based lookup.

Container adapters
- stack                                - Last-in first-out (LIFO)
- queue                              - First-in first-out (FIFO)
- priority_queue              - Highest-priority element is always the first element out
Sorting
# Example

Searching
# Example
List
# Example

Stack
# Example
Queue
# Example
Map
# Example
Set
# Example
Tree
# Example

Graph
# Example

No comments:

Post a Comment