堆变量范围

时间:2015-10-30 14:47:20

标签: c++ templates variables scope syntax-error

我正在实现一个堆,我无法理解为什么变量'int hole'不在我的insert方法的范围内。我已经尝试了很多东西,比如在方法和类中声明它。我的插入中的for循环是抛出错误的原因。

template <class Comparable>
class BinaryHeap
{
    public:
        explicit BinaryHeap( int capacity = 100 );
        bool isEmpty( ) const;
        bool isFull( ) const;
        const Comparable & findMin( ) const;
        void insert( const Comparable & x );
        void deleteMin( );
        void deleteMin( Comparable & minItem );
        void makeEmpty( );
    private:
        int currentSize; // Number of elements in heap
        vector<Comparable> array; // The heap array
        void buildHeap( );
        void percolateDown( int hole );
 };

template <class Comparable>
void BinaryHeap<Comparable>::insert( const Comparable & x )
{
    if( isFull( ) ) //throw Overflow( );
    // Percolate up
        int hole = ++currentSize;
    **for( ; hole > 1 && x < array[ hole / 2 ]; hole /= 2 )**
        array[ hole ] = array[ hole / 2 ];
        array[ hole ] = x;
}

5 个答案:

答案 0 :(得分:2)

throw注释掉后,if语句看起来像

if( isFull( ) )
{
    int hole = ++currentSize;
}

变量hole仅在&#34;范围内&#34;在if声明中。

答案 1 :(得分:2)

没有花括号({}),iffor块只包含一个语句。因此,如果我们为了清晰起见添加它们,那么您的代码实际上将等同于以下内容:

if( isFull( ) ) {
    int hole = ++currentSize;
}
for( ; hole > 1 && x < array[ hole / 2 ]; hole /= 2 ) {
    array[ hole ] = array[ hole / 2 ];
}
array[ hole ] = x;

当这样编写时,很明显为什么代码不能编译。相反,您应该自己使用花括号,以便正确表示您想要拥有的块:

if (isFull()) {
    int hole = ++currentSize;
    for (; hole > 1 && x < array[ hole / 2 ]; hole /= 2) {
        array[ hole ] = array[ hole / 2 ];
        array[ hole ] = x;
    }
}

答案 2 :(得分:1)

  1. array[ hole ] = x;位于for - 循环之外。
  2. hole仅在if( isFull( ) )的正文中声明。
  3. 应该看起来更像这样:

    int hole = /* initial value */;
    
    if( isFull( ) )
    {
        //throw Overflow( );
        // Percolate up
        hole = ++currentSize;
    }
    
    for( ; hole > 1 && x < array[ hole / 2 ]; hole /= 2 )
    {
        array[ hole ] = array[ hole / 2 ];
        array[ hole ] = x;
    }
    

答案 3 :(得分:1)

这是因为变量孔是在if子句的“then”分支内声明的。你可能会遗漏一些大括号。

答案 4 :(得分:1)

你写的地方

if( isFull( ) ) {
 //throw Overflow( );
}

我认为你的意图是

if

其他人解释了当以下一行与{ }相关联时会发生什么,因为你遗漏了那些capacity+1

顺便说一句,我注意到你正在使用基于1的索引。堆算法的大多数文档假定基于1的索引,所以我希望这就是原因。但是std :: vector使用基于0的索引。如果您的意图是浪费向量的元素0,请记住在将向量调整为capacity而不仅仅是H[i]<=H[2*i] && h[i]<=h[2*i+1]时。当我编写堆算法时,我总是重新定义基于0的索引的规则。而不是H[i]<=H[2*i+1] && h[i]<=h[2*i+2]我使用规则 - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{ [self changeBakcgroundColorOfView:navigationController.topView withColor:[UIColor redColor]]; } - (void) changeBakcgroundColorOfView:(UIView *)view withColor:(UIColor*)color{ for (UIView *subview in view.subviews) { if ([NSStringFromClass([subview class]) isEqualToString:@"UIToolbar"]) { UIToolbar *toolbar = (UIToolbar *)subview; toolbar.backgroundColor = color; toolbar.barTintColor = color; } [self changeBakcgroundColorOfView:subview withColor:color]; } }