先生がクラスのウェブサイトに次のコードを投稿しました。私はそれの働きを理解していません。もし誰かが精緻化することができれば、それは素晴らしいだろう。もう1つの理由は、なぜ彼女は戻り値としてペアを使用しています、boolだけでは十分ではありませんか?ここでバイナリ検索ツリーへの挿入
コードです:
template <class T>
std::pair<BST<T>::iterator, bool> BST<T>::insert(const T& val) {
node<T>* t = root, *parent = null;
while (t) {
if (t->val == val)
//return std::pair<BST<T>::iterator, bool>(iterator(t, this), false);
return std::make_pair(iterator(t, this), false); // stl convenience function
parent = t;
if (t->val > val) t = t->left;
else if (t->val < val) t = t->right;
}
node<T>* newNode = new node<T>(val); // (1) allocate memory
newNode->parent = parent; // (2) link child to parent
//(3) link parent to child
if (!parent) root = newNode;
else if (parent->val > val) parent->left = newNode;
else parent->right = newNode;
return std::make_pair(iterator(newNode, this), true);
}
ありがとうございました。私は今それではっきりしている! –