c++ - 访问 vector 元素的类中的 "this"指针

标签 c++ class pointers

我在一个实现文件中有以下...

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/

相关文章:

c++ - 使用 Telnet 测试 select()

c++ - 我如何构造一个仿函数以用于像 boost 的 brent_find_minima 这样的算法?

python - 通过 lambda 将变量传递到函数 Tkinter

java - 根据枚举输入返回类

c++ - 类型 ** 的包装器的数组订阅运算符的返回类型是 *&?

c++ - 试图将未知数量的字符串插入 vector ,但我的 cin 循环没有终止

c++ - 如何获取 std::chrono::time_point 时钟类型

python - 当类也在不同的模块中时从另一个模块访问对象?

c - 无法创建二叉搜索树

c++ - 结构中指针数组的析构函数