[英]Why does this static variable refuse to change?
我一直在尝试用 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)
我通过打印它们的值检查了axis
、 next
、 previous
等变量在整个程序过程中是否发生变化。 我终于得出了next_positive
和next_negative
没有改变的结论。 为什么? 为什么这些变量拒绝改变? 据我了解(第二个答案),使变量static
只是告诉编译器在其使用期间不理会它(如果返回并且其他方法需要使用它,则在方法退出后不要删除它)。 static
是否以某种方式暗示const
? 这里有什么交易?
谢谢!
当您将变量声明为静态变量(并对其进行初始化)时,它只会初始化一次。 下次遇到该变量时,它不会用新值重新初始化——您需要为其赋值。
实际上,如果每次函数运行时都需要重新初始化静态向量,那么您完全没有理由在这里使用静态向量。 如果您试图执行某种优化,那么这种优化还为时过早。
同时,替换:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.