为什么这个静态变量拒绝改变?

时间:2015-07-02 16:32:39

标签: c++ c++11 static const

我一直在尝试用C ++ 11编写一个程序,用于返回一个返回对象矢量的人工智能。为了确保在函数退出后对象不会被删除,我已经将它们设置为静态以及向量。这是方法(类中的其他方法对此无关紧要,以及我放在矢量中的对象的内部工作方式 - 所有相关的是类的名称H),以及测试函数:

//hvote.h

#ifndef __HVOTE_H_INCLUDED__
#define __HVOTE_H_INCLUDED__

#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include "h.h"

class Hvote
{
    public:
        Hvote();
        //This method is the class's constructor.
        //It sets both hs and alphas to empty vectors.
        //There are a bunch of instance variables and methods not shown here.
        std::vector<H>& find_hs(std::vector<std::vector<double>>&, std::vector<bool>&);
        //This method finds and returns the heuristics needed for the boosting algorithm.
};

#endif

//hvote.cpp

Hvote::Hvote()
{
    //some code to initialize the instance variables.
}

//some other methods.

std::vector<H>& Hvote::find_hs(std::vector<std::vector<double>>& points,
                               std::vector<bool>& answers)
{
    static std::vector<H> available_hs;
    int axes = points[0].size();
    for (int axis = 0; axis < axes; axis = axis + 1)
    {
        std::sort(points.begin(),
                  points.end(),
                  [=](std::vector<double> a, std::vector<double> b) mutable -> bool
                      {
                          return (a[axis] < b[axis]);
                      }
                 );
        double previous = points[0][axis];
        for (int datapoint = 0; datapoint < points.size() - 1; datapoint = datapoint + 1)
        {
            double next = points[datapoint + 1][axis];
            if (next != previous)
            {
                if (answers[datapoint + 1] != answers[datapoint])
                {
                    static H next_positive(axis, (next + previous)/2, true);
                    static H next_negative(axis, (next + previous)/2, false);
                    available_hs.push_back(next_positive);
                    available_hs.push_back(next_negative);
                }
            }
            previous = next;
        }
    }
    static std::vector<H>& available_hs_ref = available_hs;
    return available_hs_ref;
}

//main.cpp

#include <iostream>
#include <vector>
#include "h.h"
#include "hvote.h"

int main()
{
    Hvote hvote;
    std::vector<std::vector<double>> points;
    std::vector<bool> answers;
    for (double x = 1.0; x < 21.0; x = x + 1.0)
    {
        for (double y = 1.0; y < 21.0; y = y + 1.0)
        {
            std::vector<double> point{x, y};
            points.push_back(point);
            bool answer = (x < y);
            answers.push_back(answer);
        }
    }
    std::vector<std::vector<double>>& points_ref = points;
    std::vector<bool>& answers_ref = answers;
    std::vector<H>& hs = hvote.find_hs(points_ref, answers_ref);
    for (int i = 0; i < hs.size(); i = i + 1)
    {
        int axis = hs[i].get_axis();
        double cutoff = hs[i].get_cutoff();
        bool direction = hs[i].get_direction();
        std::cout << "Heuristic(axis = " << axis << ", cutoff = " << cutoff << ", direction = " << direction << ")" << std::endl;
    }
    return 0;
}

我期待输出中出现各种H个对象,具有不同的轴,截止点和方向,但令我惊讶的是H类的两个不同的实例(以及很多hs中出现了这两个重复的内容!这是输出:

Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)
Heuristic(axis = 0, cutoff = 1.5, direction = 1)
Heuristic(axis = 0, cutoff = 1.5, direction = 0)

我检查了axisnextprevious等。变量在整个程序过程中通过打印它们的值而发生变化。我终于得出结论,next_positivenext_negative并没有改变。为什么?为什么这些变量拒绝改变? So far as I understand(第二个答案),使变量static只是告诉编译器在其使用期间不管它(如果返回该方法退出,则不要删除它)方法将需要使用它)。 static是否暗示const?这里的交易是什么?
谢谢!

1 个答案:

答案 0 :(得分:2)

当您将变量声明为静态(并初始化)时,它只会初始化一次。下次遇到变量时,不会使用新值重新初始化 - 您需要为其分配。

实际上,如果需要在每次运行函数时重新初始化静态向量,那么你真的没有理由在这里使用静态向量。如果您尝试执行某种优化,那么这种优化还为时过早。

在此期间,请替换:

  if (answers[datapoint + 1] != answers[datapoint])
  {
     static H next_positive(axis, (next + previous)/2, true);
     static H next_negative(axis, (next + previous)/2, false);
     available_hs.push_back(next_positive);
     available_hs.push_back(next_negative);
  }

  if (answers[datapoint + 1] != answers[datapoint])
  {
     available_hs.emplace_back(axis, (next + previous)/2, true);
     available_hs.emplace_back(axis, (next + previous)/2, false);
  }

这样您至少可以利用emplace back

available_hs不应该是static,并且您在参考它时所做的奇怪事情也不应该发生;只需返回std::vector

你应该return available_hs;