我在一个实现文件中有以下...
void ClientList::interestCompare(vector<string> intr)
{
for(int index = 0; index < intr.size(); index++)
{
this->interests[index];
}
}
这在规范文件中...
class ClientList
{
private:
// A structure for the list
struct ListNode
{
char gender;
string name;
string phone;
int numInterests; // The number of interests for the client
vector<string> interests; // list of interests
string match;
struct ListNode *next; // To point to the next node
};
//more stuff
...}
是否可以使用“this”指针访问结构中的“interests” vector ?
如果是这样的话。
正如我现在所做的那样,我将一个 ListNode 指针初始化为 head 以便访问列表。我只是想知道“this”指针是否只能访问类的成员,或者它们是否可以访问类中嵌入的更深层次的 ADT 变量。
这个问题有意义吗?
最佳答案
您只在 ClientList 类中声明了一个 ListNode 类型,这并不意味着您拥有 ClientList 的一个实例。由于您已经在使用 std::vector,您可以使用 std::vector 或 std::list 而不是实现另一个列表
class ClientList
{
private:
// A structure for the list
struct Client
{
char gender;
std::string name;
std::string phone;
int numInterests; // The number of interests for the client
std::vector<string> interests; // list of interests
std::string match;
};
std::vector<Client> clients;
//more stuff
};
编辑:
如果你想比较两个列表,使用std::set_intersection ,它需要两个容器进行排序
。
void ClientList::FindClientHasCommonInterest(const vector<string>& intr)
{
for(auto client = clients.begin(); client != clients.end(); ++client)
{
std::vector<std::string> intereste_in_common;
std::set_intersection((*client).begin(), (*client).end(),
intr.begin(), intr.end(),
std::back_inserter(intereste_in_common));
if (intereste_in_common.size() >= 3)
{
// find one client
}
}
}
关于c++ - 访问 vector 元素的类中的 "this"指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14555303/