C++ C++;如何使用数据类型为pair的链表?

C++ C++;如何使用数据类型为pair的链表?,c++,linked-list,C++,Linked List,我有一个名为: 链接列表 因此,确切的声明是: LinkedList列表 假设我在列表中存储了一系列进程,并且该对的第一个数据类型(即字符串)标识了它是什么类型的进程。假设类别为Stop,则将进程存储在Stop下的列表中,如果类别为Resume,则进程存储在Resume下,依此类推。 我的问题是,我正在努力为我需要实现的其他方法成对访问这两种类型的数据。例如:我需要实现一个名为Count_category(string category)的方法,该方法将计算给定类别内的进程数,但我不知道如何执行

我有一个名为:

链接列表

因此,确切的声明是:

LinkedList列表

假设我在列表中存储了一系列进程,并且该对的第一个数据类型(即字符串)标识了它是什么类型的进程。假设类别为Stop,则将进程存储在Stop下的列表中,如果类别为Resume,则进程存储在Resume下,依此类推。 我的问题是,我正在努力为我需要实现的其他方法成对访问这两种类型的数据。例如:我需要实现一个名为Count_category(string category)的方法,该方法将计算给定类别内的进程数,但我不知道如何执行,因为我不知道如何访问第一个数据类型。到目前为止,我已经知道可以通过像class.first和class.second这样的操作来访问它,但是我不知道如何在我的案例中使用它。 救命

我包括我的linkedlist.hpp,如果您需要我的类对象,请告诉我


        #ifndef LINKED_LIST_
        #define LINKED_LIST_
  
        #include <utility> //for swap
        #include <exception>
  
 #include "Node.hpp"
  
 template<typename T>
 class LinkedList
 {
 private:
    Node<T>* head; // Pointer to first node in the chain;
                             // (contains the first entry in the list)
    int count;           // Current count of list items 
    
    // Locates a specified node in this linked list.
    // @pre  position is the number of the desired node;
    //       position >= 1 and position <= itemCount.
    // @post  The node is found and a pointer to it is returned.
    // @param position  The number of the node to locate.
    // @return  A pointer to the node at the given position.
    Node<T>* getNodeAt(int position) const;
     void swap( LinkedList& lhs, LinkedList& rhs );
  
 public:
    LinkedList();
    LinkedList(const LinkedList<T>& rhs);
    virtual ~LinkedList();
    LinkedList& operator=( LinkedList rhs );
  
    bool isEmpty() const;
    int get_count() const;
    bool insert(int newPosition, const T& newEntry);
    bool remove(int position);
    void clear();
    
    T getEntry(int position) const;
    T replace(int position, const T& newEntry);
  
 }; // end LinkedList
#endif

``````````````````````````````````````````````````````````````



##This is where I'm stuck: (it's in a different class called PManager that uses this Linked List);##


``````````````````````````````````````````````````````````````````

int PManager::count_category(std::string category) const
{
int count = 0;
    for (int i = 1; i <= theList.get_count(); i++)
    {
        if (category == (this is where I need to access the category from the pair)
    {
        count++;
    }
}
```````````````````````````````````````````````````````

#ifndef链接列表_
#定义链接列表_
#包括//用于交换
#包括
#包括“Node.hpp”
模板
类链接列表
{
私人:
Node*head;//指向链中第一个节点的指针;
//(包含列表中的第一个条目)
int count;//列表项的当前计数
//在此链接列表中查找指定的节点。
//@pre position是所需节点的编号;

//位置>=1和位置假设您从LinkedList获得一个元素(或对它的引用):

std::pair <std::string, Process> const& elem = llist.getEntry(0);
// now elem.first is a std::string
std::pair const&elem=llist.getEntry(0);
//现在elem.first是一个std::string
elem
是(对)
std::pair
的引用。然后可以使用
elem.first
(这是一个
std::string
)和
elem.second
(这是一个
过程)


请注意,
getEntry
效率低下:它返回列表中元素的副本。

您显示的界面类似于:

int main()
{
    LinkedList< std::pair< std::string, Process > > list;

    //... put in some data

    // count all elements in a given category

    std::string what{"Stop"};
    int found = 0;

    for ( int count = 0; count < list.get_count(); count++ )
    {
        std::pair< std::string, Process > element = list.getEntry( count );
        if ( element.first == what ) 
        {   
            found++;
        }   
    }

    std::cout << "Found " << found << " elements in category " << what << std::endl;
}
intmain()
{
LinkedList>列表;
//…输入一些数据
//计算给定类别中的所有元素
std::string what{“Stop”};
int=0;
对于(int count=0;countelement=list.getEntry(count);
if(element.first==什么)
{   
发现++;
}   
}

std::cout使用
std::map
不是更好吗?然后你可以问
the_map[“Stop”]。size()
例如。不幸的是,这正是我应该如何实现它的,所以我不能使用向量。然后你需要显示
LinkedList
模板的接口。在不知道它的接口的情况下,我们不知道它支持什么操作,也不知道如何调用它们。更好的是,显示你到目前为止的代码以及你被卡住的地方。好吗请给我一分钟时间,让我想一想怎么做(我是个新手:))添加了hpp文件我在哪里添加这个?另外,我想我不允许使用自动,你有其他方法吗?我已经发布了我正在使用的链接列表的hpp文件,看看这是否有帮助。是的,这是正确的,因为我知道进程的类别。但是,非巧合的是,该类别将在测试期间生成。我需要以某种方式访问配对中字符串的值进行比较。还有其他想法吗?[我完全同意这是一个糟糕的设计,这正是作业所需的,我无法更改它:(]@Genetarist这确实访问字符串并对其进行比较,这里:
element.first==what
。您可以将其调整为函数,其中
what
是parameter@Genetarist请试一试!如果你的代码有效的话,它会工作。它会比较我从你的解释中得到的你对的内容。简单地做一下:-)是的,你是对的,我只是想澄清一下,以防我不清楚我实际需要做什么。因此,当类别为==get_category函数中的what时,我需要计算在what下列出的进程数(进程本身是一个类).所以我不计算what的总数,我只计算单个what下列出的进程总数。我想你的代码实现了这一点,对吧???@Genetarist:是的!那么就试试吧!
#include <ranges>
#include <algorithm>
#include <list>
#include <iostream>

class Process{};

int main()
{
    std::list< std::pair< std::string, Process > > l{ { "Stop", {} }, {"Resume",{}},{"Stop",{}}};
    std::string what{"Resume"};
    int found = std::ranges::count_if( l | std::views::keys , [&what]( const auto&s ){return s==what;} );
    std::cout << "Found " << found << " elements in category " << what << std::endl;
}