简体   繁体   English

嵌套向量的分割错误

[英]segmentation fault using nested vectors

I stumbled upon the following code segment somewhere to create a list of random numbers in a certain interval: 我偶然发现以下代码段在某个间隔内创建了一个随机数列表:

#include <vector>
#include <iostream>
#include <math.h>
struct gen_rand_pos{
  double factor;
  public:
    gen_rand_pos(double r=1.0): factor(r/RAND_MAX)
    {}
    double operator()(){
        return rand()*factor;
    }
};

int main(){
  int N = 5;
  std::vector<double> result(N);
  std::generate_n(std::back_inserter(result), N, gen_rand_pos(1.0));
  std::cout << result[0] << std::endl;
}

It works perfectly fine. 它工作得很好。 I tried to take this one step further and do the same, but this time creating a list(vector) of random unit vectors, uniformly distributed on the sphere. 我试图将这一步骤更进一步并做同样的事情,但是这次创建了一个随机单位矢量的列表(矢量),它们均匀地分布在球体上。 Here is my go at this: 这是我的努力:

double l2_norm(std::vector<double> const& u) {
    double accum = 0.;
    for (double x : u) {
        accum += x * x;
    }
    return sqrt(accum);
}

struct gen_rand_vec{
  double factor;
  public:
    gen_rand_vec(): factor(2.0/RAND_MAX)
    {}
  std::vector<double> operator()(){
    std::vector<double> result = {rand()*factor-1,rand()*factor-1,rand()*factor-1}; // uniform in each component
    double norm = l2_norm(result);
    std::transform(result.begin(), result.end(), result.begin(),
           std::bind1st(std::multiplies<float>(),1/norm)); // normalize the vector
    return result;
  }
};

However if I now try to construct it in the same manner: 但是,如果我现在尝试以相同的方式构造它:

int main(){
  int N = 5;
  std::vector<std::vector<double>> result2(N);
  std::generate_n(std::back_inserter(result2), N, gen_rand_vec());
  std::cout << result2[0][0] <<std::endl;
}

I get a segmentation error. 我遇到了细分错误。 Is the problem lying in the back_inserter or am I doing something completely wrong? 问题出在back_inserter还是我做错了什么? I can't figure out. 我不知道。 I checked the construction of the single vectors and they work just fine, so the mistake has to lie somewhere after that. 我检查了单个向量的构造,它们工作得很好,所以此后的错误必须出在某个地方。

Thanks in advance. 提前致谢。

Default size of inner vectors for result2 for indices <0,4> is 0, so you cannot access 索引<0,4> result2的内部向量的默认大小为0,因此您无法访问

result2[0][0]   // inner vector doesn't keep any elements

when you call generate_n with back_inserter , you add new vectors to the end of result2 . 当使用back_inserter调用generate_n时,会将新的向量添加到result2的末尾。 In this case you can access indices of inner vector for result2[>=5] . 在这种情况下,您可以访问result2[>=5]的内部向量的索引。

Your problem is quite simple, std::back_inserter is inserting new vectors at the end of result2 . 您的问题很简单, std::back_inserterresult2的末尾插入新的向量。 Hence, you cannot access the first element, since it is not initialized. 因此,您无法访问第一个元素,因为它尚未初始化。 To solve this, just remove the (N) from the initialization of result2 . 要解决此问题,只需从result2的初始化中删除(N) Example . 例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM