【发布时间】: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)
我检查了axis、next、previous 等变量在整个程序过程中通过打印它们的值而发生变化。我终于得出结论,next_positive 和 next_negative 只是没有改变。为什么?为什么这些变量拒绝改变? So far as I understand(第二个答案),创建一个变量static只是告诉编译器在使用期间不要管它(如果它被返回并且其他方法需要使用它,请不要在方法退出后删除它)。 static 是否以某种方式暗示 const?这有什么关系?
谢谢!
【问题讨论】:
标签: c++ c++11 static constants