簡體   English   中英

更改結構C ++內部數組的大小

[英]Changing the size of an array inside of a struct C++

我有一個如下所示的結構。

struct thread_data{
   int staringPoint;
   int endingPoint;
   double query[];
}; 

用戶輸入一個數字,我需要輸入數字作為數組的大小。 在用戶輸入數字之后,是否可以為數組分配內存或設置該數組的大小?

我試圖這樣做:

int userNumber = 10;
struct thread_data newThreads[5];


for(int i=0; i < 5;i++){
    newThreads[i].query = new double[userNumber];
}

但我收到此錯誤消息:
數組類型“ double []”不可分配

一個簡單的解決方案是:

struct thread_data{
    int staringPoint;
    int endingPoint;
    std::vector<double> query;
}; 

// ...

for(int i=0; i < 5;i++)
    newThreads[i].query.resize(userNumber);

您分配正確,但更改了double query[]; double *query;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM