Vector::push_back不在Vector中存储值
Vector::push_back not storing values in vector
最近我一直在尝试写一个神经网络程序。我将所有的神经元连接存储在神经元的向量中。然而,每当我将连接推回向量时,它似乎不会存储(我可以通过调试模式来判断),并且当我试图在for循环中添加向量的激活值时,我就会得到out_of_range错误。这是我的代码。
Main.cpp
#include <iostream>
#include "neuron.h"
void displayboard(bool board [8][8]);
using namespace std;
int main()
{
int id = 2;
int inputids [] = {3};
int outputids [] = {4};
int inputweights [] = {5};
bool new_neuron = true;
neuron test (inputids, outputids, inputweights, new_neuron, id);
test.connections.at(0).value = 6;
// here is where the error is returned
test.activation();
cout << test.activationnumber;
return 0;
}
这里是neuron。cpp:
#include "neuron.h"
#include <vector>
#include <random>
#include <ctime>
using namespace std;
neuron::neuron(int inputids [], int outputids [], int inputweights [],
bool new_neuron, int id)
{
this->id = id;
if (new_neuron==true) {
srand (time(0));
connection tempconnection;
for (int i = 0; i <=(sizeof (inputids)/sizeof (inputids [0])); i++)
{
tempconnection.type=false;
tempconnection.tofrom = inputids [i];
tempconnection.weight = rand ();
this->connections.push_back (tempconnection);
}
// this continues on for other options
}
void neuron::activation (){
for (int i=0; i<=this->connections.size (); i++) {
this->activationnumber += ((this->connections.at(i).value)
*(this->connections.at (i).weight));
}
}
UPDATE:阅读这篇文章将帮助你理解为什么你的"sizeof/sizeof"方法在c++中不是很好。
原始回答
sizeof(array)/sizeof(array[0])
的行为可能不是你所期望的。下面的代码输出2,但您似乎期望输出4。对于堆栈中的对象使用array,对于堆中的对象使用vector。
#include <iostream>
using namespace std;
void foo( int array[] )
{
wcout << sizeof( array ) / sizeof( array[ 0 ] );
}
int main()
{
int test[ 4 ];
foo( test );
return 0;
}
改变
int inputids [] = {3};
int outputids [] = {4};
vector< int > {3};
vector< int > {4};
也改变
neuron(int inputids [],int outputids [] …
{
…
for (int i = 0; i <= …; i++)
…
tempconnection.tofrom = inputids [i];
neuron( vector< int > & inputids, vector< int > & outputids …
{
…
for( auto id : inputids )
…
tempconnection.tofrom = id;
相关文章:
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 在std::vector上存储带有模板的类实例
- std::vector move 而不是交换到空 vector 并释放存储
- 将可变参数模板类存储到 std::vector 中
- 在 std::vector<无符号字符中存储任意数据的方法>
- 是否可以将具有不同签名的 lambda 存储在 std::vector 中并在函数中执行它们(使用各自的参数)?
- 将数据从 std::Vector 存储到 Eigen::Vector 时出错
- 如何将自定义特征张量类存储到 std::vector 中?
- 如何在 std::vector 中存储来自 std:<short>:vector <uint8_t>的数据
- 如何在OpenCV中存储部分轮廓点喜欢新矢量中的左侧,该类型为<vector<vector<Point>>
- 如何从 Pyhon 端对存储在 std::vector 中的对象进行持久更改,作为 std::shared_ptr
- 使用 std::vector<char> 作为异构记录的存储是否安全?
- 在 std::vector<std::vector 中重新存储内部向量<TYPE>>
- 从 CSV 读取并存储在 Vector 中
- 代码将向量存储为向量<vector>,为什么没有错误消息?
- 使用运算符重载添加存储在 vector 中的类对象
- 如何在文件中节省空间地存储和检索 std::vector<int> 值
- 从std::vector存储到std::set,其中vector包含一个结构体,而std::set只包含该结构体中的一个
- 使用vector存储实体
- c++在循环期间将vector存储在数组中