如何插入结构内分配的 vector ?
代码是我写的

struct state{
  int time_taken;
  vector<int>time_live(1);
  string loc_name;
  vector<string>loc(1);
};
state A[100];

A[flag1]->loc.insert(flag2);
A[flag1]->time_live.insert(time);

A[flag2]->loc.insert(flag1);
A[flag2]->loc.insert(time);

我得到的错误是这个
kris_UCS.cpp:11: error: expected identifier before numeric constant
kris_UCS.cpp:11: error: expected ‘,’ or ‘...’ before numeric constant
kris_UCS.cpp:13: error: expected identifier before numeric constant
kris_UCS.cpp:13: error: expected ‘,’ or ‘...’ before numeric constant
kris_UCS.cpp: In function ‘int main()’:
kris_UCS.cpp:60: error: base operand of ‘->’ has non-pointer type ‘state’
kris_UCS.cpp:61: error: base operand of ‘->’ has non-pointer type ‘state’
kris_UCS.cpp:63: error: base operand of ‘->’ has non-pointer type ‘state’
kris_UCS.cpp:64: error: base operand of ‘->’ has non-pointer type ‘state’

最佳答案

struct state
{
    int time_taken;
    vector<int> time_live; // no parenthesis
    string loc_name;
    vector<string> loc; // no parenthesis
};

state A[100];

A[flag1].loc.push_back(flag2);
A[flag1].time_live.push_back(time);

A[flag2].loc.push_back(flag1);
A[flag2].loc.push_back(time);

关于c++ - 在C++中插入结构内的 vector 时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3769020/

10-11 15:36