C++ 为什么这个静态变量拒绝更改?
我一直在尝试用C++11为一些人工智能编写一个程序,返回一个对象向量。为了确保对象在函数退出后不会被删除,我将它们与向量一起设置为静态。这是类中的其他方法对此不重要的方法,以及我在向量中放置的对象的内部工作方式-所有相关的是类的名称H以及测试函数:C++ 为什么这个静态变量拒绝更改?,c++,c++11,static,constants,C++,C++11,Static,Constants,我一直在尝试用C++11为一些人工智能编写一个程序,返回一个对象向量。为了确保对象在函数退出后不会被删除,我将它们与向量一起设置为静态。这是类中的其他方法对此不重要的方法,以及我在向量中放置的对象的内部工作方式-所有相关的是类的名称H以及测试函数: //hvote.h #ifndef __HVOTE_H_INCLUDED__ #define __HVOTE_H_INCLUDED__ #include <iostream> #include <algorithm> #i
//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;
}
我检查了axis、next、previous等变量,通过打印它们的值,它们在整个程序过程中都会发生变化。我最终得出结论,next_阳性和next_阴性都没有改变。为什么?为什么这些变量拒绝改变?第二个答案是,将变量设为静态只是告诉编译器在使用过程中不要使用它,如果返回了该变量,则在方法退出后不要删除它,其他方法将需要使用它。静态是否意味着常量?这是怎么回事?
谢谢 当您将一个变量声明为静态并初始化它时,它只会初始化自身一次。下次遇到该变量时,不会使用新值重新初始化它-您需要为其赋值 实际上,如果每次函数运行时都需要重新初始化静态向量,那么您根本没有理由在这里使用静态向量。如果您试图执行某种优化,那么这种优化是不成熟的 同时,替换:
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);
}
与
这样你至少可以利用回阵地的机会
可用的_hs不应该是静态的,你正在做的关于它的奇怪的事情也不应该发生;只需返回一个std::vector
您应该返回可用的\u hs
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);
}