【问题标题】:Why does this static variable refuse to change?为什么这个静态变量拒绝改变?
【发布时间】:2015-07-02 16:32:39
【问题描述】:

我一直在尝试用 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?这有什么关系?
谢谢!

【问题讨论】:

    标签: c++ c++11 static constants


    【解决方案1】:

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

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

    同时,替换:

      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;

    【讨论】:

      猜你喜欢
      • 2014-10-28
      • 1970-01-01
      • 2021-11-03
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 2011-09-13
      相关资源
      最近更新 更多