有人可以向我解释一下:
(((((*(parent))->leaves))[7])->zone_id)
Run Code Online (Sandbox Code Playgroud)
指针的指针对我来说真的很困惑.这是我在调试模式下从手表中得到的表达式.我试图访问zone_id树的最后一个元素内的字符串(有10个元素,每个元素为不同的数字0-9).
编辑:这是整个搜索功能,希望它足以理解:
string Tree::search(string str, node** parent, int num) {
int value;
if (num < str.length()) {
value = boost::lexical_cast<int> (str.substr(num, 1));
if ((*parent)->leaves[value] != NULL && num != str.length() -1) {
search (str, &((*parent)->leaves[value]), num+1);
} else if (num == str.length() -1) {
if ( (*(parent)->leaves)[value]->zone_id.empty() )
cout<<"Yep.";
else
return (string) "No_results.";
}
}
}
Run Code Online (Sandbox Code Playgroud)
和结构:
struct node {
string zone_id;
node* leaves [10];
};
Run Code Online (Sandbox Code Playgroud)
好吧,让我们摆脱一些括号来简化它:
(*parent)->leaves[7]->zone_id
Run Code Online (Sandbox Code Playgroud)
现在它更容易理解.我们是dereferencing parent(*parent),它为我们提供了一个指向某个对象的指针,该对象具有一个被调用的数组成员leaves.所以我们使用索引7访问该数组的元素,这给了我们另一个指针,这次指向一个有一个成员调用的对象zone_id.然后我们访问该zone_id成员.
这都是假设没有涉及运算符重载.
图解(箭头是"指向"):
________ _________ ___________ ___________
| parent |-->| *parent |-->| struct: | ,-->| struct: |
|________| |_________| | leaves[0] | | | zone_id |
| leaves[1] | | | ... |
| leaves[2] | |
| leaves[3] | |
| leaves[4] | |
| leaves[5] | |
| leaves[6] | |
| leaves[7] | --'
| leaves[8] |
| ... |
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
93 次 |
| 最近记录: |