简体   繁体   English

c++:结构内 std::vector 的 std::vector

[英]c++: std::vector of std::vector inside a struct

I need to create a struct that sotres a std::vector<std::vector<int>> that will contain 4 vectors of ints.我需要创建一个包含 4 个整数向量的std::vector<std::vector<int>>的结构。 I tried the declaration I usually use for vector of vectors:我尝试了通常用于向量向量的声明:

struct myStruct{
   std::vector<std::vector<int>> myVector(4);
};

but, when I compile, I get this error:但是,当我编译时,我得到了这个错误:

myProgram.cpp:79:52: error: expected identifier before numeric constant
     std::vector<std::vector<int>> myVector(4);
                                           ^
myProgram.cpp:79:52: error: expected ‘,’ or ‘...’ before numeric constant

I tried to declare the vector in the struct and then reserve 4 elements in the main(), in the following way:我尝试在结构中声明向量,然后在 main() 中保留 4 个元素,方法如下:

struct myStruct{
   std::vector<std::vector<int>> myVector;
};

int main(){
   myStruct s;
   s.myVector.reserve(4);
   s.myVector[0].push_back(1);
   return 0;
}

In this way it compiles without errors, but I get a segmentation violation as soon as I try to push_back.通过这种方式,它编译时没有错误,但是一旦我尝试 push_back,我就会遇到分段违规。

What is the proper way to do this task?执行此任务的正确方法是什么? And why can't I use the first declaration to specify the size of myVector?为什么我不能使用第一个声明来指定 myVector 的大小? Thank you: :)谢谢: :)

A default member initializer inside a class or struct must have an = token and/or { curly braces } . class 或结构中的默认成员初始化程序必须具有=标记和/或{花括号}

std::vector<std::vector<int>> myVector{4};

[Note this would be trickier if the type were just std::vector<int> , since curly braces for a vector imply a sequence of elements. [请注意,如果类型只是std::vector<int> ,这将更加棘手,因为vector的花括号意味着一系列元素。 std::vector<int>{4} is a vector with size one whose one element is 4 , not a vector of four zeroes. std::vector<int>{4}是一个大小为 1 的向量,其一个元素是4 ,而不是四个零的向量。 But here it's fine because {4} can't convert to std::initializer_list<std::vector<int>> , so that constructor overload isn't eligible, and the vector(size_type) constructor does win.]但是这里很好,因为{4}不能转换为std::initializer_list<std::vector<int>> ,因此构造函数重载不符合条件,并且vector(size_type)构造函数确实获胜。]

The example program has undefined behavior because reserve does not change the size or create any elements .示例程序具有未定义的行为,因为reserve不会更改大小或创建任何元素 So s.myVector[0] is invalid since the vector is still empty.所以s.myVector[0]是无效的,因为向量仍然是空的。 Remember, reserve is just a setup hint for the vector.请记住, reserve只是向量的设置提示。 Any valid program that uses reserve would still be valid if you removed all the reserve calls.如果您删除了所有reserve调用,任何使用reserve的有效程序仍然有效。

Using resize instead would do what you seem to mean: make the size of myVector equal to 4, by creating 4 empty element vectors.改用resize会达到您的意思:通过创建 4 个空元素向量,使myVector的大小等于 4。 Then the push_back would add the number 1 to the first of those vectors, resulting in data {{1}, {}, {}, {}} .然后push_back会将数字 1 添加到这些向量中的第一个,从而产生数据{{1}, {}, {}, {}}

int main(){
   myStruct s;
   s.myVector.resize(4);
   s.myVector[0].push_back(1);
   return 0;
}
struct myStruct {
    std::vector<std::vector<int>> myVector{4};
};

int main() {
    myStruct s;
    s.myVector[0].push_back(1);
    s.myVector[0].push_back(2);
    s.myVector[0].push_back(3);
    s.myVector[0].push_back(4);

    s.myVector[1].push_back(11);
    s.myVector[1].push_back(21);
    s.myVector[1].push_back(31);
    s.myVector[1].push_back(41);

    cout << s.myVector[0][0] << " " << s.myVector[0][1] << " " << s.myVector[0][2] << " " << s.myVector[0][3] << " " << endl;
    cout << s.myVector[1][0] << " " << s.myVector[1][1] << " " << s.myVector[1][2] << " " << s.myVector[1][3] << " " << endl;

    return 0;
}

What is the proper way to do this task?执行此任务的正确方法是什么?

Hope this will answer your question.希望这能回答你的问题。

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

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