gpt4 book ai didi

c++ - 指向 vector 和数组指针的指针

转载 作者:太空宇宙 更新时间:2023-11-04 11:58:07 25 4
gpt4 key购买 nike

我有一个映射将一个字符映射到一个二维规则数组来处理该字符。以下是代码:

struct CFG //this struct is data structure to contain a context free grammar
{
vector<char> *V;
vector<char> *T;
map<char,vector<vector<rhs>*>*> *prod;
// char start;
};

int main()
{
map<char,vector<vector<rhs>*>*> *prod; //rhs is the type of each cell of the 2-d array
CFG cfg1;
cfg1.V= new vector<char>;

//We imput elements into the V array here......//

cfg1.prod= new map<char,vector<vector<rhs>*>*>;

for(int i=0;i<cfg1.V->size();i++)
{
vector<vector<rhs>*>* all_prod_of_one_nonter= new vector<vector<rhs>*>;
*(cfg1.prod)[*(cfg1.V)[i]]=all_prod_of_one_nonter; //error occurs here//////
}
}

在我标记为'error occurs'的行中,出现如下错误:

q1.cpp: In function ‘int main()’:
q1.cpp:93:29: error: no match for ‘operator*’ in ‘**(cfg1.CFG::V + ((unsigned int)(((unsigned int)i) * 12u)))’

我使用 * 取消对指针 cfg1.V 的引用,以便我可以使用下标符号来访问数组单元格。如何消除错误?

最佳答案

(*cfg1.prod)[(*cfg1.V)[i]]=all_prod_of_one_nonter;  

(原因:operator[](数组下标)比operator*(间接)绑定(bind)更紧密。)

关于c++ - 指向 vector 和数组指针的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15255515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com