std :: vector从一行代码清空到下一行

时间:2018-02-03 16:03:30

标签: c++ vector

我目前正在编写需要矢量的东西。我有两个嵌套循环;

for (size_t i = 0; i < m_neurons.size(); i++) {
        for (size_t j = 0; j < m_neurons[0].m_weights.size(); j++) {

        }
}

这部分是我们感兴趣的部分:

    d.out ("i == " + std::to_string (i));
    d.out ("m_neurons.size() == " + std::to_string (m_neurons.size()));
    d.out ("m_neurons[i].m_weights.size() == " + std::to_string (m_neurons.at(i).m_weights.size()));
    d.out ("m_neurons[i].m_weights.size() == " + std::to_string (m_neurons.at(i).m_weights.size()));
    for (size_t j = 0; j < m_neurons[0].m_weights.size(); j++) {

我打印出一些尺寸然后,我希望C ++循环遍历矢量;够容易吧?

除非发生这种情况:

i == 0
m_neurons.size() == 1
m_neurons[i].m_weights.size() == 4
m_neurons[i].m_weights.size() == 4
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
Aborted (Speicherabzug geschrieben)
[maximilian@new-host library]$ 

1)不知何故,矢量被清空了 2)不知何故,数据的收集被视为我使用.at (int index)

我有:

  • 尝试在vector.at()vector[]
  • 之间轮流进行
  • 删除了可执行文件并重新编译(多次)
  • 重新启动我的电脑[dunno,可能有帮助]
  • 使用GDB进行调查(向量将值从一步更改为另一步)
  • 利用我平庸的谷歌搜索技巧搜索类似的东西。
  • 对这些载体的内容进行了一些介绍

我知道:

  • 之间的矢量未更改
  • 正是这行代码错误[因为我之后完全打印了]
  • 我记得它在当天早些时候工作了

现在我的问题是:

  1. 这是一个已知的问题/功能吗?
  2. 是什么造成的?
  3. 我该如何解决?
  4. 编辑:

    我制作了一些可以解决问题的代码。似乎工作正常。问题是:我的代码和那里的代码之间没有任何变化。 什么可能是触发错误的东西?如上所述:在dbg中,向量的大小从一步变为另一步

    #include <iostream> 
    #include <vector>
    #include "debugMachine.h"
    
    class bar {
        public:
        bar (std::vector<double> i) {
            foobarmiz = i;
        }
        std::vector<double> foobarmiz;
    };
    
    class foo {
        public:
        std::vector<bar> foobar;
        void test (std::vector<double>& expected) {
            d.out ("HI");
            for (size_t i = 0; i < foobar.size(); i++) {
                d.out ("HI2");
                d.out ("i == " + std::to_string (i));
                d.out ("m_neurons.size() == " + std::to_string (foobar.size()));
                d.out ("m_neurons[i].m_weights.size() == " + std::to_string (foobar.at(i).foobarmiz.size()));
                d.out ("m_neurons[i].m_weights.size() == " + std::to_string (foobar.at(i).foobarmiz.size()));
                for (size_t j = 0; j < foobar[0].foobarmiz.size(); j++) {
                    d.info ("Gonna change weights");
                    foobar.at(i).foobarmiz[j] +=1; 
                }
                d.out ("out of backward");
            }
        }
    } f;
    
    int main () {
        d.write = true;
        std::vector<double> test {1.0f, 2.0f, 3.0f, 5.34f};
        f.foobar.push_back (bar (test));
        f.foobar.push_back (bar (test));
        f.foobar.push_back (bar (test));
        f.test (test);
    }
    

    这是错误的功能的完整性:

    void CLayer::m_backward (std::vector<double>& expected) {
        d.out ("HI");
        for (size_t i = 0; i < m_neurons.size(); i++) {
            d.out ("HI2");
            d.out ("i == " + std::to_string (i));
            d.out ("m_neurons.size() == " + std::to_string (m_neurons.size()));
            d.out ("m_neurons[i].m_weights.size() == " + std::to_string (m_neurons.at(i).m_weights.size()));
            d.out ("m_neurons[i].m_weights.size() == " + std::to_string (m_neurons.at(i).m_weights.size()));
            for (size_t j = 0; j < m_neurons[0].m_weights.size(); j++) {
                d.info ("Gonna change weights");
                d.info ("m_weights before : " + std::to_string(m_neurons.at(i).m_weights.at(j)));
                m_neurons.at(i).m_weights[j] -= (0.003f * -(m_expected.at(i) - m_output.at(i)) * m_output.at(i) * (1 - m_output.at(i)) * m_input.at(j)); 
                d.info ("m_weights after : " + std::to_string(m_neurons[i].m_weights[j]));
            }
            d.out ("out of backward");
        }
    }
    

1 个答案:

答案 0 :(得分:0)

这是提出问题的示例。

#include <string>
#include <iostream> 
#include <vector>

class bar {
public:
    bar(std::vector<double> i) {
        foobarmiz = i;
    }
    std::vector<double> foobarmiz;
};

class foo {
public:
    std::vector<bar> foobar;
    void test(std::vector<double>& expected) {
        std::cout << ("HI");
        for (size_t i = 0; i < foobar.size(); i++) {
            std::cout << ("HI2") << std::endl;
            std::cout << ("i == " + std::to_string(i)) << std::endl;
            std::cout << ("m_neurons.size() == " + std::to_string(foobar.size())) << std::endl;
            std::cout << ("m_neurons[i].m_weights.size() == " + std::to_string(foobar.at(i).foobarmiz.size())) << std::endl;
            std::cout << ("m_neurons[i].m_weights.size() == " + std::to_string(foobar.at(i).foobarmiz.size())) << std::endl;
            for (size_t j = 0; j < foobar[0].foobarmiz.size(); j++) { // because of this line
                std::cout << ("Gonna change weights") << std::endl;
                foobar.at(i).foobarmiz[j] += 1;
            }
            std::cout << ("out of backward") << std::endl;
        }
    }
} f;

int main()
{
    std::vector<double> test{ 1.0f, 2.0f, 3.0f, 5.34f };
    f.foobar.push_back(bar(test));
    std::vector<double> test2{ 1.0f, 23.3f, 2.1f };
    f.foobar.push_back(bar(test2));
    std::vector<double> test3{ 1.0f, 23.3f };
    f.foobar.push_back(bar(test3));
    f.test(test);
}