STL的相关知识

vector:

vector中实现了某种特殊的元素实现了支持auto访问
vector支持bigin function ,迭代器返回第一个元素,
还要支持 end function,迭代器返回最后一个元素

class A{
public:
  A(int n=5):n(n){
   data=new int[n];
}
int *begin(){
return data;
}
 int *end(){
return data+n;
}
private:
 int*data,n;
 };
int main(){
 A a;
 for(auto x:a){
   cout<<x<<endl;
}
for(auto iter=a.begin();iter!=a.end();iter++){
   auto x=*iter;
   cout<<x<<endl;
}
return 0;
}
实现二叉排序树的auto访问
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <vector>
using namespace std;

#define BEGINS(x) namespace x {
#define ENDS(x) }

BEGINS(test1)

int main() {
    set<int> s;
    for (int i = 0; i < 5; i++) {
        int val = rand() % 100;
        cout << "insert : " << val << endl;
        s.insert(val);
    }
    for (auto x : s) {
        cout << "output : " << x << endl;
    }
    return 0;
}

ENDS(test1)

BEGINS(test2)

class base_node {
public :
    base_node(
        int key = 0,
        base_node *father = nullptr,
        base_node *lchild = nullptr,
        base_node *rchild = nullptr
    ) : key(key), father(father), lchild(lchild), rchild(rchild) {}
    virtual ~base_node() {}
    void print() {
        cout << key << " | ";
        if (lchild) cout << lchild->key; 
        else cout << "0";
        cout << " , ";
        if (rchild) cout << rchild->key; 
        else cout << "0";
        cout << endl;
        return ;
    }
    int key;
    base_node *father, *lchild, *rchild;

};

class binary_search_node : public base_node {
public :
    typedef base_node node_t;
    binary_search_node(
        int key = 0, 
        node_t *father = nullptr, 
        node_t *lchild = nullptr, 
        node_t *rchild = nullptr
    ) : base_node(key, father, lchild, rchild) {}
    static node_t *insert(node_t *father, node_t *root, int key) {
        if (root == nullptr) return binary_search_node::getNewNode(father, key);
        if (key == root->key) return root;
        if (key < root->key) {
            root->lchild = insert(root, root->lchild, key);
        } else {
            root->rchild = insert(root, root->rchild, key);
        }
        return root;
    }
    static node_t *getNewNode(node_t *father, int key) {
        return new binary_search_node(key, father);
    }
    static node_t *next(node_t *ptr) {
        if (ptr->rchild) {
            ptr = ptr->rchild;
            while (ptr->lchild) ptr = ptr->lchild;
            return ptr;
        }
        while (ptr->father && ptr != ptr->father->lchild) {
            ptr = ptr->father;
        }
        return ptr->father;
    }
};

class binary_search_node_iterator {
public :
    friend void output_tree(binary_search_node_iterator &);
    typedef base_node node_t;
    typedef binary_search_node_iterator iterator;
    binary_search_node_iterator(node_t *ptr) : ptr(ptr) {}
    bool operator!=(const iterator &iter) {
        return ptr != iter.ptr;
    }
    iterator &operator++() { // ++i
        ptr = binary_search_node::next(ptr);
        return *this;
    }
    iterator operator++(int) { // i++
        iterator ret(ptr);
        ptr = binary_search_node::next(ptr);
        return ret;
    }
    int &operator*() {
        return ptr->key;
    }

private:
    node_t *ptr;
};

class bs_tree {
public :
    typedef base_node node_t;
    typedef binary_search_node_iterator iterator;
    void insert(int key) {
        root.lchild = binary_search_node::insert(&root, root.lchild, key);
        return ;
    }
    node_t *get_root() { return root.lchild; }
    iterator begin() {
        node_t *p = &root;
        while (p->lchild) p = p->lchild;
        return iterator(p);
    }
    iterator end() { return iterator(&root); }

private:
    binary_search_node root;
};

void output_tree(base_node *p) {
    if (p == nullptr) return ;
    p->print();
    cout << "p next ======" << endl;
    binary_search_node::next(p)->print();
    cout << "p next print done ======" << endl << endl;
    output_tree(p->lchild);
    output_tree(p->rchild);
    return ;
}

int main() {
    bs_tree t;
    for (int i = 0; i < 5; i++) {
        int val = rand() % 100;
        cout << "insert : " << val << endl;
        t.insert(val);
    }
    for (auto x : t) {
        cout << x << endl;
    }
    return 0;
}
ENDS(test2)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值