Skip to content

Commit 13d0ba2

Browse files
committed
add rb-tree and hashtable comments
1 parent 23bf3c0 commit 13d0ba2

File tree

8 files changed

+186
-245
lines changed

8 files changed

+186
-245
lines changed

5_STL_associated_container/5_2_3_stl_tree.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ __insert(base_ptr x_, base_ptr y_, const Value& v) {
726726
link_type y = (link_type) y_;
727727
link_type z;
728728

729-
// key_compare 是键值大小比较准则。是个 function object
729+
// key_compare 默认是 less<Key>,比较第一个元素是否小于第二个元素
730730
if (y == header || x != 0 || key_compare(KeyOfValue()(v), key(y))) {
731731
z = create_node(v); // 产生一个新节点
732732
left(y) = z; // 这使得当 y 为 header 时,leftmost() == z
@@ -763,7 +763,7 @@ rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::insert_equal(const Value& v)
763763
while (x != 0) { // 从根节点开始,往下寻找适当的插入点
764764
y = x;
765765
x = key_compare(KeyOfValue()(v), key(x)) ? left(x) : right(x);
766-
// 以上,遇 “大” 则往左,遇 “小于或等于” 则往右
766+
// key_compare 默认是 less<Key>,比较第一个元素是否小于第二个元素
767767
}
768768
return __insert(x, y, v);
769769
// 以上,x 为新值插入点,y 为插入点之父节点,v 为新值
@@ -781,8 +781,9 @@ rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::insert_unique(const Value& v)
781781
bool comp = true;
782782
while (x != 0) { // 从根节点开始,往下寻找适当的插入点
783783
y = x;
784-
comp = key_compare(KeyOfValue()(v), key(x)); // v 键值小于目前节点之键值?
785-
x = comp ? left(x) : right(x); // 遇 “大” 则往左,遇 “小于或等于” 则往右
784+
// key_compare 默认是 less<Key>,比较第一个元素是否小于第二个元素
785+
comp = key_compare(KeyOfValue()(v), key(x));
786+
x = comp ? left(x) : right(x);
786787
}
787788
// 离开 while 循环之后,y 所指即插入点之父节点(此时的它必为叶节点)
788789

@@ -793,6 +794,7 @@ rb_tree<Key, Value, KeyOfValue, Compare, Alloc>::insert_unique(const Value& v)
793794
// 以上,x 为插入点,y 为插入点之父节点,v 为新值
794795
else // 否则(插入点之父节点不为最左节点)
795796
--j; // 看不懂这个含义???
797+
// key_compare 默认是 less<Key>,比较第一个元素是否小于第二个元素
796798
if (key_compare(key(j.node), KeyOfValue()(v)))
797799
// 新键值不与既有节点之键值重复,于是以下执行安插操作
798800
return pair<iterator,bool>(__insert(x, y, v), true);

5_STL_associated_container/5_3_set-test.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66

77
using namespace std;
88

9-
int main() {
9+
int main()
10+
{
1011
int i;
1112
int ia[5] = {0, 1, 2, 3, 4};
1213
set<int> iset{ia, ia + 5};
1314

15+
cout << less<int>()(1, 2) << endl;
16+
1417
cout << "size=" << iset.size() << endl;
1518
cout << "3 count =" << iset.count(3) << endl;
1619
iset.insert(3);
@@ -28,7 +31,8 @@ int main() {
2831

2932
set<int>::iterator ite1 = iset.begin();
3033
set<int>::iterator ite2 = iset.end();
31-
for (; ite1 != ite2; ++ite1) {
34+
for (; ite1 != ite2; ++ite1)
35+
{
3236
cout << *ite1;
3337
}
3438
cout << endl;
@@ -52,4 +56,6 @@ int main() {
5256
cout << "1 not found" << endl;
5357

5458
// *ite1 = 9; // 修改失败
59+
60+
return 0;
5561
}

0 commit comments

Comments
 (0)