2

我在实现文件中有以下内容......

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”向量?

如果有怎么办。

正如我现在所拥有的,我初始化一个 ListNode 指针指向 head 以便访问列表。我只是想知道“this”指针是否只能访问类的成员,或者它们是否可以访问嵌入在类中的更深层次的 ADT 变量。

这个问题还有意义吗?

4

2 回答 2

4

您只在 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,它需要两个容器sorted就位。

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
       }
   }  
}
于 2013-01-28T04:16:42.160 回答
3

不,对于嵌套类,Java 和 C++ 是不同的。C++ 嵌套类本质上与Java 中的静态嵌套类相同。因此,您必须使用嵌套结构的实例来访问其成员。

于 2013-01-28T04:20:22.440 回答