手写堆之后,发现在自己的 namespace 内没有 using std::swap,也没有定义函数 swap 的情况下,编译通过而且程序可以正常运行(AC),非常迷惑,希望有大佬帮忙解答。
还想知道这个不用 std:: 的 swap 函数是哪个库里的。
#include<bits/stdc++.h>
namespace sun{
template<typename T>
struct Heap_V{
int cnt;std::vector<T> w;
Heap_V(){cnt=0,w.resize(1);}
T top(){return w[1];}
void repair_up(const int &x){
if(x==1||w[x/2]<w[x]) return;
swap(w[x/2],w[x]),repair_up(x/2);
}
void push(const T &x){w.push_back(x),repair_up(++cnt);}
void repair_down(const int &x){
if(x*2>cnt) return;
if(x*2+1<=cnt&&w[x*2+1]<w[x*2]){
if(w[x*2+1]<w[x]) swap(w[x*2+1],w[x]),repair_down(x*2+1);
}
else if(w[x*2]<w[x]) swap(w[x*2],w[x]),repair_down(x*2);
}
void pop(){swap(w[1],w[cnt--]),w.pop_back(),repair_down(1);}
int size(){return cnt;}
bool empty(){return cnt==0;}
};
}