STL源碼剖析 set集合

  • set的特性是 所有的元素會按照鍵值自動排序
  • set 的鍵值等同于實值
  • set不允許涵蓋兩個相同的鍵值
  • 不可以通過迭代器修改set的元素數值,這會破壞元素的排列順序。因此set<T>::iterator 被定義為底層RB-tree的const_iterator,杜絕寫入。也就是set的iterators是一種constant iterators 相對于mutable iterators
  • set類似list,當客戶端對其進行元素的新增或者刪除操作的時候,操作之前的迭代器不會失效,但是被操作的迭代器會失效
  • STL提供了一組set/multiset的相關算法,包括交集set_intersection 聯集 set_union 差集 set_difference 對稱差集set_symmetric_difference
  • set利用RB-Stree的排序機制,因此是基于紅黑樹進一步的函數封裝
#include <iostream>#ifdef __STL_USE_EXCEPTIONS
#define __STL_TRY   try
#define __STL_UNWIND(action)   catch(...) { action; throw; }
#else
#define __STL_TRY
#define __STL_UNWIND(action)
#endif# ifdef __STL_EXPLICIT_FUNCTION_TMPL_ARGS
# define __STL_NULL_TMPL_ARGS <>
# else
# define __STL_NULL_TMPL_ARGS
# endiftypedef bool __rb_tree_color_type;
const __rb_tree_color_type __rb_tree_red = false;  //紅色為0
const __rb_tree_color_type __rb_tree_black = true; //黑色為1struct __rb_tree_node_base{typedef __rb_tree_color_type color_type;typedef __rb_tree_node_base* base_ptr;color_type color;   //節點顏色 非紅即黑base_ptr parent;    //RB-Tree的許多操作 都是需要父節點的參與base_ptr left;      //指向左節點base_ptr right;     //指向右節點static base_ptr minimum(base_ptr x){while (x->left != 0){//二叉搜索樹的特性//一直向左走 就會找到最小值x = x->left;}return x;}static base_ptr maximum(base_ptr x){while(x->right != 0){//二叉搜索樹的特性//一直向右走 就會找到最大值x = x->right;}return x;}
};template <class Value>
struct __rb_tree_node : public __rb_tree_node_base{typedef __rb_tree_node<Value>* link_type;Value value_field; //節點值
};//基層迭代器
struct __rb_tree_base_iterator{typedef __rb_tree_node_base::base_ptr base_ptr;typedef std::bidirectional_iterator_tag iterator_category;typedef ptrdiff_t difference_type;base_ptr node; //用于和容器之間產生一個連接關系(make a reference)//以下實現于operator++內,因為再無其他地方會調用這個函數了void increment(){if (node->right != 0){        //如果有右節點,狀況一node = node->right;       //就向右走while(node->left != 0){   //然后一直往左子樹前進node = node->left;    //即為答案}} else{                       //沒有右節點 狀況二base_ptr y = node->parent;//找出父節點while(node == y->right){  //如果現行節點本身是個右子節點node = y;             //需要一直上溯  直到不為右子節點為止y = y->parent;}if (node->right != y){    //如果此時node的右子節點不等于此時的父節點node = y;             //狀況三 此時的父節點即為解答}                         //否則 此時的node即為解答  狀況四}/*以上判斷 "若此時的右子節點不等于此時的父節點"是為了應對一種特殊的情況* 即:欲尋找的根節點的下一個節點,而此時根節點恰好沒有右子節點* 當然,上述的特殊做法需要配合RB-Tree根節點和特殊的header之間的特殊的關系*/}//以下實現于operator--內,因為再無其他地方會調用這個函數了void decrement(){//狀況一//如果是紅節點 且 父節點的父節點等于自己 右子節點即為答案//狀況一 發生于node為header的時候(亦即node為end()時)//注意:header的右子節點 即 mostright指向整棵樹的max節點if (node->color == __rb_tree_red && node->right->right == node){node = node->right;}//狀況二//如果有左子節點 令y指向左子節點;只有當y有右子節點的時候 一直往右走 到底,最后即為答案else if (node->left!=0){base_ptr y = node->left;while (y->right != 0){y = y->right;}node = y;} else{//狀況三//既不是根節點 也沒有左節點//找出父節點,當現行的節點身為左子節點時,一直交替往上走,直到現行節點不是左節點為止base_ptr y = node->parent;while(node == y->left){node = y;y = y->right;}node = y; //此時父節點即為答案}}
};//RB-Tree的正規迭代器
template <class Value,class Ref,class Ptr>
struct __rb_tree_iterator : public __rb_tree_base_iterator{typedef Value value_type;typedef Ref reference;typedef Ptr pointer;typedef __rb_tree_iterator<Value,Value&,Value*> iterator;typedef __rb_tree_iterator<Value,const Value&,const Value*> const_iterator;typedef __rb_tree_iterator<Value,Value&,Value*> self;typedef __rb_tree_node<Value>* link_type;__rb_tree_iterator(){}__rb_tree_iterator(link_type x){node = x;}__rb_tree_iterator(const iterator& it){node = it.node;}reference operator* () const {return link_type(node)->value_field; }#ifndef __SGI_STL_NO_ARROW_OPERATORreference operator->()const {return &(operator*());}
#endif //__SGI_STL_NO_ARROW_OPERATORself& operator++(){increment();return *this;}self operator++(int){self tmp = *this;increment();return tmp;}self& operator--(){decrement();return *this;}self operator--(int){self tmp = *this;decrement();return tmp;}};template<class T,class Alloc>
class simple_alloc{
public:static T* allocate(std::size_t n){return 0==n?0:(T*)Alloc::allocate(n * sizeof(T));}static T* allocate(void){return (T*)Alloc::allocate(sizeof (T));}static void deallocate(T* p,size_t n){if (n!=0){Alloc::deallocate(p,n * sizeof(T));}}static void deallocate(T* p){Alloc::deallocate(p,sizeof(T));}
};namespace Chy{template <class T>inline T* _allocate(ptrdiff_t size,T*){std::set_new_handler(0);T* tmp = (T*)(::operator new((std::size_t)(size * sizeof (T))));if (tmp == 0){std::cerr << "out of memory" << std::endl;exit(1);}return tmp;}template<class T>inline void _deallocate(T* buffer){::operator delete (buffer);}template<class T1,class T2>inline void _construct(T1 *p,const T2& value){new(p) T1 (value);  //沒看懂}template <class T>inline void _destroy(T* ptr){ptr->~T();}template <class T>class allocator{public:typedef T           value_type;typedef T*          pointer;typedef const T*    const_pointer;typedef T&          reference;typedef const T&    const_reference;typedef std::size_t size_type;typedef ptrdiff_t   difference_type;template<class U>struct rebind{typedef allocator<U>other;};pointer allocate(size_type n,const void * hint = 0){return _allocate((difference_type)n,(pointer)0);}void deallocate(pointer p,size_type n){_deallocate(p);}void construct(pointer p,const T& value){_construct(p,value);}void destroy(pointer p){_destroy(p);}pointer address(reference x){return (pointer)&x;}const_pointer const_address(const_reference x){return (const_pointer)&x;}size_type max_size()const{return size_type(UINT_MAX/sizeof (T));}};
}template <class Key,class Value,class KeyOfValue,class Compare,class Alloc>
class rb_tree{
protected:typedef void* void_pointer;typedef __rb_tree_node_base* base_ptr;typedef __rb_tree_node<Value> rb_tree_node;typedef simple_alloc<rb_tree_node,Alloc>rb_tree_node_allocator;typedef __rb_tree_color_type color_type;
public:typedef Key key_type;typedef Value value_type;typedef value_type* pointer;typedef const value_type* const_pointer;typedef value_type& reference;typedef const value_type& const_reference;typedef rb_tree_node* link_type;typedef std::size_t size_type;typedef std::ptrdiff_t difference_type;
protected:link_type get_node(){return rb_tree_node_allocator::allocate();}void put_node(link_type p){return rb_tree_node_allocator::deallocate(p);}link_type create_node(const value_type& x){link_type tmp = get_node(); //配置空間__STL_TRY{Chy::allocator<Key>::construct(&tmp->value_field,x);//構造內容};__STL_UNWIND(put_node(tmp));return tmp;}link_type clone_node(link_type x){//復制一個節點的顏色和數值link_type tmp = create_node(x->value_field);tmp->color = x->color;tmp->left = 0;tmp->right = 0;return tmp;}void destroy_node(link_type p){Chy::allocator<Key>::destroy(&p->value_field); //析構內容put_node(p); //釋放內存}
protected://RB-tree只使用三筆數據表現size_type node_count; //追蹤記錄樹的大小 (節點的數量)link_type header;     //實現上的小技巧Compare key_compare;  //節點之間的鍵值大小的比較準則. 應該會是一個function object//以下三個函數用于方便獲取header的成員link_type& root() const{return (link_type&)header->parent;}link_type& left_most() const{return (link_type&)header->left;}link_type& right_most() const{return (link_type&)header->right;}//以下六個函數用于方便獲得節點x的成員static link_type& left(link_type x){ return(link_type&)x->left;}static link_type& right(link_type x){ return(link_type&)x->right;}static link_type& parent(link_type x){ return(link_type&)x->parent;}static reference value(link_type x){ return x->value_field;}static const Key& key(link_type x){ return KeyOfValue()(value(x));}static color_type& color(link_type x){return (color_type&) (x->color);}//獲取極大值和極小值 node class有實現此功能static link_type minimum(link_type x){return (link_type) __rb_tree_node_base::minimum(x);}static link_type maximum(link_type x){return (link_type) __rb_tree_node_base::maximum(x);}//迭代器typedef __rb_tree_iterator<value_type,reference,pointer>iterator;
public:iterator __insert(base_ptr x,base_ptr y,const value_type& v);link_type __copy(link_type x,link_type p);void __erase(link_type x);void clear() {if (node_count != 0) {__erase(root());left_most() = header;root() = 0;right_most() = header;node_count = 0;}}void init(){header = get_node(); //產生一個節點空間 令header指向它color(header) = __rb_tree_red;//令header為紅色 用于區分header和root,在iterator的operator++中root() == 0;left_most() = header;   //令header的左子節點等于自己right_most() = header;  //令header的右子節點等于自己}
public://allocation / deallocationrb_tree(const Compare& comp = Compare()): node_count(0),key_compare(comp){init();}~rb_tree(){clear();put_node(header);}rb_tree<Key,Value,KeyOfValue,Compare,Alloc>&operator==(const rb_tree<Key,Value,KeyOfValue,Compare,Alloc>&x);
public://accessorsCompare key_comp() const {return key_compare;}iterator begin() {return left_most();} //RB樹的起頭為最左(最小)節點處iterator end(){return header;} //RB樹的終點為header所指處bool empty() const {return node_count == 0;}size_type size() const {return node_count;}size_type max_size() const {return size_type (-1);}
public://insert/erase//將x插入到RB-Tree中 (保持節點的獨一無二)std::pair<iterator,bool> insert_unique(const value_type& x);//將x插入到RB-Tree中 (允許節點數值重復)iterator insert_equal(const value_type& x);//尋找鍵值為k的節點iterator find(const value_type& k);
};//插入新的數值 節點鍵值允許重復
//注意:返回的是一個RB-Tree的迭代器,指向的是新增的節點
template <class Key,class Value,class KeyOfValue,class Compare,class Alloc>
typename rb_tree<Key,Value,KeyOfValue,Compare,Alloc>::iterator
rb_tree<Key,Value,KeyOfValue,Compare,Alloc>::insert_equal(const value_type &v) {link_type y = header;link_type x = root(); //根節點開始while(x != 0){        //根節點開始 從上往下尋找適當的插入節點y = x;//如果當前根節點比 輸入的v大,則轉向左邊,否則轉向右邊x = key_compare(KeyOfValue()(v), key(x)) ? left(x) : right(x);}//x為新值插入點 y為插入點的父節點 v為新值return __insert(x,y,v);
}//插入新的數值 節點鍵值不允許重復
//注意:返回結果是pair類型,第一個元素是一個RB-Tree的迭代器,指向的是新增的節點;第二個參數表示插入成功與否
template <class Key,class Value,class KeyOfValue,class Compare,class Alloc>
std::pair<typename rb_tree<Key,Value,KeyOfValue,Compare,Alloc>::iterator,bool>
rb_tree<Key,Value,KeyOfValue,Compare,Alloc>::insert_unique(const value_type &v) {link_type y = header;link_type x = root(); //從根節點開始bool comp = true;while(x != 0){ //從根節點開始 往下尋找適當的插入點y = x;comp = key_compare(KeyOfValue()(v), key(x)); //v鍵值小于目前節點的鍵值x = comp ? left(x) : right(x); //遇"大"則向左 遇"小"則向右}//離開while循環之后 y所指的即 插入點之父節點(此時它必為葉子結點)iterator j = iterator(y); //迭代器j指向插入點的父節點yif (comp){//如果while循環時候,判定comp的數值,如果comp為真(表示遇到大的元素,將插入左側)//如果插入節點的父節點是最左側的節點//x為插入點,y為插入節點的父節點,v表示新值if (j == begin()){return std::pair<iterator,bool>(__insert(x,y,v), true);} else{//插入節點的父節點不是最左側的節點//調整j 回頭準備測試--j;}if (key_compare(key(j.node),KeyOfValue()(v))){//小于新值(表示遇到小的數值,將插在右側)return std::pair<iterator,bool>(__insert(x,y,v), true);}}//至此 表示新值一定和樹中的鍵值重復 就不應該插入新的數值return std::pair<iterator,bool>(j, false);
}//真正的插入執行程序 __insert()
template <class Key,class Value,class KeyOfValue,class Compare,class Alloc>
typename rb_tree<Key,Value,KeyOfValue,Compare,Alloc>::iterator
rb_tree<Key,Value,KeyOfValue,Compare,Alloc>::__insert(base_ptr x_, base_ptr y_, const value_type &v) {//參數x_為新的插入點 參數y_為插入點的父節點 參數v為新值link_type x = (link_type)x_;link_type y = (link_type)y_;link_type z ;//key_compare 是鍵值大小的比較準則,應該是一個function objectif (y == header||x != 0||key_compare(KeyOfValue()(v),key(x))){z = create_node(v); //產生一個新的節點//當y即為header的時候,leftmost = z;if (y == header){root() = z;right_most() = z;} else if (y == left_most()){//y為最左節點//維護leftmost() 使其永遠指向最左節點left_most() = z;} else{z = create_node(v);//產生一個新的節點//讓新節成為插入點的父節點y的右子節點right(y) = z;if (y == right_most()){ //維護rightmost()讓其永遠指向最右的節點right_most() = z;}}parent(z) = y; //設定新節點的父節點left(z) = 0; //設定新節點的左子節點right(z) = 0; //設定新節點的右子節點//修改顏色//參數一為新增節點 ;參數二 為root__rb_tree_rebalance(z,header->parent);++node_count;//返回一個迭代器 指向新的節點return iterator(z);}
}//全局函數
//新節點必須為紅色,如果插入出父節點同樣為紅色,就需要進行樹形旋轉
inline void __rb_tree_rotate_left(__rb_tree_node_base* x,__rb_tree_node_base*& root){//x為旋轉點__rb_tree_node_base* y = x->right;//令y為旋轉點的右子節點x->right = y->left;if (y->left != 0){//回馬槍設定父親節點y->left->parent = x;}y->parent = x->parent;//令y完全替代x的地位 (需要將x對其父節點的關系完全接收回來)if (x == root){root = y; //x為根節點} else if (x == x->parent->left){x->parent->left = y;  //x為其父節點的左子節點} else{x->parent->right = y; //x為其父節點的右子節點}y->left = x;x->parent = y;
}//全局函數
//新節點必須為紅色,如果插入出父節點同樣為紅色,就需要進行樹形旋轉
inline void __rb_tree_rotate_right(__rb_tree_node_base* x,__rb_tree_node_base*& root){//x為旋轉點__rb_tree_node_base* y = x->left; //y為旋轉點的左子節點x->left = y->right;if (y->right != 0){y->right->parent = x;}y->parent = x->parent;//令y完全替代x的地位if(x == root){root = y;} else if (x == x->parent->right){x->parent->right = y;} else{x->parent->left = y;}y->parent = x;x->parent = y;
}//調整RB_tree 插入節點之后,需要進行調整(顏色/翻轉)從而滿足要求
inline void __rb_tree_balance(__rb_tree_node_base* x,__rb_tree_node_base*& root){x->color = __rb_tree_red; //新節點的顏色必須是紅色的while(x!=root && x->parent->color == __rb_tree_red){//父節點為紅色的//父節點為祖父節點的左子節點if (x->parent == x->parent->parent->left){//令y為伯父節點__rb_tree_node_base* y = x->parent->right;if (y && y->color == __rb_tree_red){ //伯父節點存在 且為紅x->parent->color = __rb_tree_black;//更改父節點的顏色為黑y->color = __rb_tree_black;//更改父節點的顏色為黑x->parent->parent->color = __rb_tree_red;//更改祖父節點的顏色為紅x = x->parent->parent;} else{//無伯父節點 或者伯父節點的顏色為黑if (x == x->parent->right){//新節點為父節點的右子節點x = x->parent;//第一次參數為左旋節點__rb_tree_rotate_left(x,root);}x->parent->color = __rb_tree_black;//改變顏色x->parent->parent->color = __rb_tree_red;//第一次參數為右旋節點__rb_tree_rotate_right(x->parent->parent,root);}} else{//父節點為祖父節點的右子節點__rb_tree_node_base* y = x->parent->parent->left; //令y為伯父節點if (y && y->color == __rb_tree_red){ //存在伯父節點,且為紅x->parent->color = __rb_tree_black;//更改父節點為黑y->color = __rb_tree_black;//更改伯父節點為黑x->parent->parent->color = __rb_tree_red; //更改祖父節點為紅x = x->parent->parent; //準備繼續往上層檢查} else{//無伯父節點 或者伯父節點 為黑if (x == x->parent->left){//新節點 為 父節點的左子節點x = x->parent;__rb_tree_rotate_right(x,root); //第一參數為右旋轉點}x->parent->color = __rb_tree_black;//改變顏色x->parent->parent->color = __rb_tree_red;//第一參數為左旋點__rb_tree_rotate_left(x->parent->parent,root);}}} //while結束root->color = __rb_tree_black;
}//元素查找程序
template <class Key,class Value,class KeyOfValue,class Compare,class Alloc>
typename rb_tree<Key,Value,KeyOfValue,Compare,Alloc>::iterator
rb_tree<Key,Value,KeyOfValue,Compare,Alloc>::find(const value_type &k) {link_type y = header; //last node which is  not less than klink_type x = root(); //current nodewhile(x != 0){//key_compare 是節點大小的比較準則 function objectif (!key_compare(key(x),k)){//進行到這里 表示x的數值大于k 。遇到大的數值向左走y = x,x = left(x);} else{x = right(x);}}iterator j = iterator (y);return (j == end() || key_compare(k,key(j.node))) ? end() : j;
}/************************************************************************************************************************/
//注意:以下的identify定義于
template <class T>
struct identify : public std::unary_function<T,T>{const T& operator()(const T& x) const {return x;}
};template <class Key,class Alloc,class Compare = std::less<Key>>
class set{
public:typedef Key key_type;typedef Key value_type;//注意key_compare 和 value_compre 使用同一個比較函數typedef Compare key_compare;typedef Compare value_compare;
private:typedef rb_tree<key_type,value_type,identify<value_type>,key_compare,Alloc>rep_type;rep_type t; //使用紅黑樹(RB-Tree)實現set
public:typedef typename rep_type::const_pointer pointer;typedef typename rep_type::const_pointer const_pointer;typedef typename rep_type::const_reference reference;typedef typename rep_type::const_reference const_reference;//set迭代器無法執行寫入操作,因為set的元素有一定的次序安排,不允許用戶在任意處進行寫入操作typedef typename rep_type::const_iterator iterator;typedef typename rep_type::const_uterator const_iterator;typedef typename rep_type::const_reverse_iterator reverse_iterator;typedef typename rep_type::const_reverse_iterator const_reverse_iterator;typedef typename rep_type::size_type size_type;typedef typename rep_type::difference_type difference_type;//allocation/deallocation//set使用RB-Tree的insert_unique() 要求插入的元素都不可以重復//multiset 使用 insert_equal() 允許相同鍵值的存在//構造函數set():t(Compare()){}explicit set(const Compare& comp) : t(comp){}template<class InputIterator>set(InputIterator first,InputIterator last) : t(Compare()){t.insert_unique(first,last);}template<class InputIterator>set(InputIterator first,InputIterator last,const Compare& comp) : t(comp){t.insert_unique(first,last);}set(const set<Key,Compare,Alloc>&x):t(x.t){}set<Key,Compare,Alloc>& operator=(const set<Key,Compare,Alloc>&x){t = x.t;}//set進行函數的包裝//accessorskey_compare key_comp()const{ return t.key_comp();}//需要注意 set使用的value_comp 事實上為RB-Tree的key_comp()value_compare value_comp() const {return t.key_comp();}iterator begin() const {return t.begin();}iterator end() const {return t.end();}reverse_iterator rbegin() const{return t.rbegin();}reverse_iterator rend() const{return t.rend();}bool empty() const {return t.empty();}size_type size() const {return t.size();}size_type max_size() const {return t.max_size();}void swap(set<Key,Compare,Alloc>& x){t.swap(x.t);}//insert / erasetypedef std::pair<iterator,bool>pair_iterator_bool;std::pair<iterator,bool>insert(const value_type& x){std::pair<typename rep_type::iterator,bool> p = t.insert_unique(x);return std::pair<iterator,bool>(p.first,p.second);}iterator insert(iterator position,const value_type& x){typedef typename rep_type::iterator rep_iterator;return t.insert_unique((rep_iterator&)position,x);}template <class InputIterator>void insert(InputIterator first,InputIterator last){t.insert_unique(first,last);}void erase(iterator position){typedef typename rep_type::iterator rep_iterator;t.erase((rep_iterator&)position);}size_type erase(const key_type& x){return t.erase(x);}void erase(iterator first,iterator last){typedef typename rep_type::iterator rep_iterator;t.erase((rep_iterator&)first,(rep_iterator&)last);}void clear(){t.clear();}//set operationsiterator find(const key_type& x)const {return t.find(x);}size_type count(const key_type& x)const {return t.count(x);}iterator lower_bound(const key_type&x) const{ return t.lower_bound(x);}iterator upper_bound(const key_type&x) const{ return t.upper_bound(x);}std::pair<iterator,iterator>equal_range(const key_type&x)const{return t.equal_range(x);}//以下的__STL_NULL_TMPL_ARGS 被定義為<>friend bool operator== __STL_NULL_TMPL_ARGS (const set&,const set&);friend bool operator< __STL_NULL_TMPL_ARGS (const set&,const set&);
};template <class Key,class Compare,class Alloc>
inline bool operator==(const set<Key,Compare,Alloc>&x,const set<Key,Compare,Alloc>&y){return x.t == y.t;
}template <class Key,class Compare,class Alloc>
inline bool operator<(const set<Key,Compare,Alloc>&x,const set<Key,Compare,Alloc>&y){return x.t < y.t;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/446243.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/446243.shtml
英文地址,請注明出處:http://en.pswp.cn/news/446243.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python turtle畫圣誕樹動圖_圣誕節!教你用Python畫棵圣誕樹

作者 | 糖甜甜甜&#xff0c;985高校經管研二&#xff0c;擅長用 Python、R、tableau 等工具結合統計學和機器學習模型做數據分析。 如何用Python畫一個圣誕樹呢&#xff1f; 最簡單&#xff1a; 1height 5 2 3stars 1 4for i inrange(height): 5print(( * (height - i)) (** …

java藍橋杯 試題-基礎練習-十六進制轉八進制

試題-基礎練習-十六進制轉八進制 題目 試題 基礎練習 十六進制轉八進制 資源限制 時間限制&#xff1a;1.0s 內存限制&#xff1a;512.0MB 問題描述   給定n個十六進制正整數&#xff0c;輸出它們對應的八進制數。 輸入格式   輸入的第一行為一個正整數n &#xff08;1…

STL源碼剖析 map

所有元素會根據元素的鍵值自動被排序 元素的類型是pair&#xff0c;同時擁有鍵值和實值&#xff1b;map不允許兩個元素出現相同的鍵值pair 代碼 template <class T1,class T2> struct pair{typedef T1 first_type;typedef T2 second_type;T1 first; //publicT2 secon…

java api接口怎么寫_Java 如何設計 API 接口,實現統一格式返回?

來源&#xff1a;老顧聊技術前言接口交互返回格式控制層Controller美觀美化優雅優化實現方案前言在移動互聯網&#xff0c;分布式、微服務盛行的今天&#xff0c;現在項目絕大部分都采用的微服務框架&#xff0c;前后端分離方式&#xff0c;(題外話&#xff1a;前后端的工作職責…

java 輸出學生成績和成績等級

題目 從鍵盤讀入學生成績&#xff0c;找出最高分&#xff0c;并輸出學生成績等級。?成績>最高分-10 等級為’A’?成績>最高分-20 等級為’B’?成績>最高分-30 等級為’C’?其余 等級為’D’提示&#xff1a;先讀入學生人數&#xff0c;根據人數創建int數組&#…

STL源碼剖析 multiset 和 multimap

multiset和set完全相同&#xff0c;唯一的差別在于允許鍵值的重復&#xff0c;因此底層操作使用的是紅黑樹的insert_equal() 而不是insert_unique()multimap和map完全相同&#xff0c;唯一的差別在于允許鍵值的重復&#xff0c;因此底層操作使用的是紅黑樹的insert_equal() 而不…

java 二維數組

聲明和初始化 靜態初始化 // 靜態初始化&#xff1a; // 一維數組int[] arr1_1 {1, 2, 4};System.out.println(Arrays.toString(arr1_1)); // 二維數組int[][] arr1_2 {{1, 2}, {4, 5}, {9, 10}};for (int[] i :arr1_2) {System.out.print(Arrays.toS…

STL源碼剖析 hashtable

二叉搜索樹具有對數平均時間的表現&#xff0c;但是這個需要滿足的假設前提是輸入的數據需要具備隨機性hashtable 散列表這種結構在插入、刪除、搜尋等操作層面上也具有常數平均時間的表現。而且不需要依賴元素的隨機性&#xff0c;這種表現是以統計為基礎的 hashtable的概述 …

append在python里是什么意思_“一棵綠蘿七個鬼”是什么意思?臥室里到底能不能養綠蘿!...

很多人都喜歡在家里養盆綠蘿&#xff0c;一是能凈化室內空氣&#xff0c;讓家里綠意濃濃&#xff0c;更有生機一些&#xff1b;二是綠蘿好養&#xff0c;水培土培都行&#xff0c;養著也省心。在養花界有一句俗語&#xff1a;“一棵綠蘿七個鬼”&#xff0c;這句話是什么意思呢…

java 二分查找

注意 二分查找要求原數組為有序序列&#xff0c;從小到大 遞歸解法 public class problem9 {public static void main(String[] args) {int[] arr {1,2,3,4,6,7};int left 0;int right arr.length - 1;int value 2;System.out.println(Arrays.toString(arr));int index …

C++for_each| bind1st | ptr_fun | std::function的用法

c for_each 用法_小鍵233-CSDN博客 傳入參數 要傳入參數給global function ,需要使用 ptr_fun() 這個 function adapter 將global function 轉成function object , 然后再用bind2nd() 將參數bind成一個function object。&#xff08;這句話好拗口&#xff09; void fun(int i…

java三個柱子漢諾塔問題

題目 移動盤子&#xff0c;每一次只能移動一個&#xff0c;小盤子在大盤子上。 打印1 from A to B過程 注意 1&#xff09;盤子編號的變化和輔助柱子的變化 2&#xff09;當盤子編號為1時&#xff0c;結束遞歸&#xff0c;此時移動結束 代碼 package p2;/*** Illustratio…

python遍歷txt每一行_python – 計算(和寫入)文本文件中每一行的...

第一次在堆棧中發布 – 總是發現以前的問題足以解決我的問題&#xff01;我遇到的主要問題是邏輯……即使是偽代碼答案也會很棒. 我正在使用python從文本文件的每一行讀取數據,格式如下&#xff1a; This is a tweet captured from the twitter api #hashtag http://url.com/si…

java楊輝三角形

題目 代碼1 public class YangHuiTriangle {public static void main(String[] args) {print(10);}public static void print(int num) {int[][] arr new int[num][];for (int i 0; i < num; i) { // 第一行有 1 個元素, 第 n 行有 n 個元素arr[i] new int[i…

python子類繼承父類屬性實例_python – 從子類內的父類訪問屬性

在類定義期間,沒有任何繼承的屬性可用&#xff1a; >>> class Super(object): class_attribute None def instance_method(self): pass >>> class Sub(Super): foo class_attribute Traceback (most recent call last): File "", line 1, in cl…

STL源碼剖析 算法開篇

STL源碼剖析 算法章節 算法總覽_CHYabc123456hh的博客-CSDN博客 質變算法 質變算法 - 會改變操作對象的數值&#xff0c;比如互換、替換、填寫、刪除、排列組合、分隔、隨機重排、排序等 #include <iostream> #include <vector>int main(){int ia[] {22,30,20,34…

java 隨機數一維數組

題目1 創建一個長度為6的int型數組&#xff0c;要求數組元素的值都在1-30之間&#xff0c;且是隨機賦值。同時&#xff0c;要求元素的值各不相同 代碼1 public class ArrayTest2 {public static void main(String[] args) {generateArray(6);}public static void generateAr…

STL源碼剖析 數值算法 accumulate | adjacent_difference | inner_product | partial_sum | power | itoa

//版本1 template <class InputIterator,class T> T accumulate(InputIterator first,InputIterator last,T init){for(;first ! last; first){init *first; //將每個元素數值累加到init身上}return init; }//版本2 template <class InputIterator,class T,class Bin…

python官網網址是什么意思_大家都是怎么部署python網站的?

flaskgunicornnginx 作者&#xff1a;Python小白 鏈接&#xff1a;centos下通過gunicorn和nginx部署Flask項目 - Python小白的文章 - 知乎專欄 來源&#xff1a;知乎 著作權歸作者所有。商業轉載請聯系作者獲得授權&#xff0c;非商業轉載請注明出處。 之前用Flask寫了個解析Tu…

java回形數矩陣

題目 從鍵盤輸入一個整數&#xff08;1~20&#xff09; 則以該數字為矩陣的大小&#xff0c;把1,2,3…n*n 的數字按照順時針螺旋的形式填入其中。例如&#xff1a; 輸入數字2&#xff0c;則程序輸出&#xff1a; 1 2 4 3 輸入數字3&#xff0c;則程序輸出&#xff1a; 1 2 3 8…