简体   繁体   English

C ++:struct in struct,push_back error

[英]C++: vector in struct, push_back error

I am trying to save the data in a vector in structs. 我试图将数据保存在结构中的向量中。 Since I do not know the length of each data, I used push_back . 由于我不知道每个数据的长度,我使用了push_back However, the for loop does not work and I got Command terminated after the first i loop. 但是, for loop不起作用,我在第一个i循环后Command terminatedCommand terminated

I tried several answers like this , but none of them works. 我试了好几种答案像这样 ,但他们没有工作。

Here is a simplified version of what I tried (and gives the same error): 这是我尝试的简化版本(并给出相同的错误):

#include <iostream>
#include <vector>

using namespace std;

typedef struct{
    vector<int> words;
}DATA_STRUCT;

void add(DATA_STRUCT *data){
    for(int i=0; i<5; i++){
        for(int s=0;s<3; s++){
            (&data)[i] -> words.push_back(s);
            cout << i << s << endl;
        }
    }
}

int main(){
    DATA_STRUCT *data = new DATA_STRUCT[5];
    add(data);
    cout << (&data)[0] -> words[0] << endl;
}

The problem lies with: 问题在于:

(&data)[i] -> words.push_back(s);

&data evaluates to a DATA_STRUCT **data . &data计算为DATA_STRUCT **data By using (&data)[i] , you are accessing memory that you are not supposed to, which causes undefined behavior. 通过使用(&data)[i] ,您正在访问您不应该访问的内存,这会导致未定义的行为。

That line can be a lot simpler. 这条线可以简单得多。

data[i].words.push_back(s);

You are almost there. 你快到了。 Here is the correct approach :- 这是正确的方法: -

#include <iostream>
#include <vector>

using namespace std;

typedef struct{
    vector<int> words;
}DATA_STRUCT;

void add(DATA_STRUCT *data){
    for(int i=0; i<5; i++){
        for(int s=0;s<3; s++){
            data[i].words.push_back(s);  //<-- edit
            cout << i << s << endl;
        }
    }
}

int main(){
    DATA_STRUCT *data = new DATA_STRUCT[5]; 
    add(data);
    cout << data[0].words[0] << endl;  //<-- edit
    delete[] data;                     //Thank juanchopanz
}

Live demo 现场演示

&data is wrong, you want the value. &data错误,你想要的价值。
data[xxx] works like the "get value" operation. data[xxx]作用类似于“获取值”操作。 (roughly speaking) (粗略地说)

Edit 编辑

There is also memory leak at :- 还有内存泄漏: -

DATA_STRUCT *data = new DATA_STRUCT[5];  

It is new without delete , so I also add delete[] data; 它是新的没有删除 ,所以我还添加了delete[] data; . (Thank juanchopanza ) (感谢juanchopanza

Edit2 EDIT2

To answer Tom de Geus :- 要回答Tom de Geus : -

Any way to avoid the new here? 有什么办法可以避免这里新的?

Here is the better version :- 这是更好的版本: -

DATA_STRUCT data[5];
add(data);
cout << data[0].words[0] << endl;

Live demo 现场演示

Edit2 EDIT2

To answer additional question from OP :- 要回答OP的其他问题: -

Why can we use arrow instead of dot? 为什么我们可以使用箭头代替点? Is data in the add function a struct of pointer? add函数中的数据是指针的结构吗?

data is DATA_STRUCT* dataDATA_STRUCT*
(&data) is DATA_STRUCT** (&data)DATA_STRUCT**
(&data)[i] is DATA_STRUCT* (&data)[i]DATA_STRUCT*
(&data)[i]-> is DATA_STRUCT (&data)[i]->DATA_STRUCT
(&data)[i]->words is DATA_STRUCT::words (&data)[i]->wordsDATA_STRUCT::words

It is a wrong address, but in term of (only) syntax, it is correct. 这是一个错误的地址,但就(仅)语法而言,它是正确的。
Wrong address ~ undefined behavior = can work OK / work but wrong / crash 错误的地址〜未定义的行为=可以正常工作/工作但错误/崩溃

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

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