【问题标题】:the "this" pointer in classes accessing vector elements访问向量元素的类中的“this”指针
【发布时间】:2013-01-28 04:09:56
【问题描述】:

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

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 变量。

这个问题有意义吗?

【问题讨论】:

  • 您必须通过ListNode 的实例来访问它。拥有ClientList 的实例并不一定意味着您拥有ListNode 的实例。
  • *this 在该函数的上下文中最好引用ClientList 的成员变量而不是ClientList::ListNode。或者那是你缺少的那块?
  • 你能解释一下你想用interestCompare做什么吗?
  • 我有两个不同的列表,男性和女性。兴趣比较();从客户那里接收兴趣向量,假设是男性,然后搜索女性 ClientList 对象以确定是否存在具有 3 个或更多匹配兴趣的客户。

标签: c++ class pointers


【解决方案1】:

您只在 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
       }
   }  
}

【讨论】:

  • 这看起来很有趣......虽然我不确定这是否适用于这种情况。我需要搜索两个不同的链接列表,参数的目的是有一个基本列表来比较搜索。我可能误读了这个,但我看不出这个函数中的基本兴趣列表是如何的,因此它可以搜索一个完全不同的链接列表。
  • 我想我关注 set_intersection 中发生的事情。看起来它指向客户端列表的开头和结尾,然后获取兴趣向量的开头和结尾进行比较,然后我不确定back_insterter在做什么。另外,“auto client =" 是否将 vector 客户端分配给它,以便稍后的指针可以访问数据?
  • back_insterter 将共同兴趣插入到新向量中。 auto 是新的c++11关键字,它推导出tyep,否则你必须写for(std::vector&lt;Client&gt;::iterator client = clients.begin(); client != clients.end(); ++client)
  • 我想我会更多地玩这个......我不确定我是否遵循所有这些,但我有足够的时间去玩它......感谢您的洞察力!
【解决方案2】:

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

【讨论】:

  • 所以如果我明白你在说什么,我将不得不这样做: ListNode *nodePtr;节点Ptr =头; nodePtr->兴趣[索引];为了访问向量
  • 没错,你需要有一个ListNode的实例。
  • 其实Java内部类也不行,因为是多对一的关系,所以外部类找不到内部类的实例。 (但是内部类知道外部类的特定实例。)
猜你喜欢
  • 2018-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-27
  • 2016-01-14
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
相关资源
最近更新 更多