简体   繁体   English

c ++初始化结构向量的正确方法

[英]c++ right way to initialize a vector of a struct

i searched a lot here, but there is no right explanation for me, for an advanced newbie in c++.我在这里搜索了很多,但是对于 C++ 中的高级新手,我没有正确的解释。 I worked before with vector of structs and now I get segmentation faults...我以前使用过结构向量,现在我遇到了分段错误...

Thats why I want to know how such objects actually works and if it is the right the way I am doing!这就是为什么我想知道这些对象实际上是如何工作的,以及我的做法是否正确!

I have a struct like我有一个像

struct numberOfSpecies {
   int predator;
   int prey1;
   int prey2;
};

and a vector of it:和它的向量:

std::vector<numberOfSpecies> size;

Before I resize it and fill it with values.在我调整它的大小并用值填充它之前。

size.resize(100);

what is actually this doing?这实际上在做什么? Is this right for a struct?这适合结构吗? It looks like it is initialized with zeros...看起来它是用零初始化的......

Now I am doing this like:现在我这样做:

size[t].predator=0;
size[t].prey1=0;
size[t].prey2=0;

for(int k = 0; k < N; ++k){
  size[t].predator++;
  size[t].prey1++;
  size[t].prey2++;
}

Is this right?这是正确的吗? Where are possible issues?可能的问题在哪里? How to do it better?如何做得更好?

The easiest and 'correct' solution here is probably to just use the resize() function that belongs to the vector object with aggregate initialization (if you have access to c++11 and on), something like这里最简单和“正确”的解决方案可能是使用属于带有聚合初始化的向量对象的resize()函数(如果您可以访问 c++11 及以上),例如

size.resize(100,{0,0,0}); //aggregate initialization 

for(int k = 0; k < N; ++k)
{
    size[t].predator++;
    size[t].prey1++;
    size[t].prey2++;
}

All members of each numberOfSpecies object will be initialized to 0.每个numberOfSpecies对象的所有成员都将初始化为 0。

This:这个:

size[t].predator=0;
size[t].prey1=0;
size[t].prey2=0;

will write zeros to the t th element of size - that may or may not be useful:将零写入sizet个元素 - 这可能有用也可能没有用:

This:这个:

for(int k = 0; k < N; ++k){
  size[t].predator++;
  size[t].prey1++;
  size[t].prey2++;
}

will increment the t th element of size N times.将第t个元素的size N 次。 This seems incredibly unlikely to be useful.这似乎不太可能有用。 I think what you want is:我想你想要的是:

size[0].predator=0;  // Technically not needed because .resize()
size[0].prey1=0;     // will have initialized it to zero anyway
size[0].prey2=0;     // *BUT* explicit is always better than implicit.

// Initialize each element of size to be one greater than previous.
for(int k = 1; k < N; ++k){
  size[k].predator = size[k-1].predator + 1;
  size[k].prey1    = size[k-1].prey1    + 1;
  size[k].prey2    = size[k-1].prey2    + 1;;
}

Use the value parameter for static parameters.将 value 参数用于静态参数。

#include <vector>

struct foo{
    int g;
    int h;
    int l;
};

int main()
{
    std::vector<foo> manyFoo(10, {0});
    manyFoo.resize(60, {0});
} 

If you want to grow your vector as you also put arbitrary values into the struct you could ->如果你想增加你的向量,因为你还可以将任意值放入结构中 ->

#include <iostream>
#include <vector>

struct foo{
    foo(int aG,int aH, int aL):g(aG),h(aH),l(aL) {};
    int g;
    int h;
    int l;
};

int main() {
    std::vector<foo> lVec;
    for (int i=0;i<10;i++) {
        lVec.emplace_back(foo(i,i*2,i*4));
    }
    int lPos=0;
    for (auto &rFoo: lVec) {
        std::cout << "Item pos" << lPos++ << " g:" << rFoo.g << " h:" << rFoo.h << " l:" << rFoo.l << std::endl;
    }
    return EXIT_SUCCESS;
}

If you know the size of the vector and you want to populate it you could ->如果您知道向量的大小并且想要填充它,您可以 ->

#include <iostream>
#include <vector>

struct foo{
    foo(int aG,int aH, int aL):g(aG),h(aH),l(aL) {};
    int g;
    int h;
    int l;
};

int main() {
    std::vector<foo> lVec(10,{0,0,0});
    int lPos = 0;
    for (auto &rFoo: lVec) {
        rFoo = foo(lPos,lPos*2,lPos*4);
        lPos++;
    }
    lPos=0;
    for (auto &rFoo: lVec) {
        std::cout << "Item pos" << lPos++ << " g:" << rFoo.g << " h:" << rFoo.h << " l:" << rFoo.l << std::endl;
    }
    return EXIT_SUCCESS;
}

You could add an default constructor to your structure.您可以向结构中添加默认构造函数。 The new code will look something like this:新代码如下所示:

struct numberOfSpecies {
    numberOfSpecies (): predator(0), prey1(0), prey2(0) { }  // default constructor
    int predator;
    int prey1;
    int prey2;
};

That way, your structure will be properly initialized inside your vector when resize is applied.这样,当应用调整大小时,您的结构将在您的向量中正确初始化。

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

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