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)
}