我有一个名为:的链表
链接列表<std::对<std::string,(名为Process的类(>gt;列表;
因此,确切的声明是:
LinkedList<std::对<std::string,进程> >假设我在列表中存储了一系列Process,该对的第一个数据类型(即string(确定了它是哪个类别的进程。假设类别为Stop,则它将Process存储在Stop下的列表中,如果类别为Resume,则Process将存储在Resume下,依此类推。我的问题是,我很难访问成对的两种类型的数据,以获得需要实现的其他方法。例如:我需要实现一个名为Count_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++;
}
}
```````````````````````````````````````````````````````
假设您从LinkedList:获得一个元素(或对它的引用(
std::pair <std::string, Process> const& elem = llist.getEntry(0);
// now elem.first is a std::string
CCD_ 1是CCD_。然后可以使用elem.first
(它是std::string
(和elem.second
(它是一个Process
(。
请注意,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;
}
LinkedList类的设计非常糟糕,原因是:
- 没有迭代器,因此不能与标准算法一起使用
- getter方法返回完整副本,效率低下,不可能更改数据
- 访问方法必须遍历所有元素,这是绝对不可接受的
- 构造函数不允许将数据放入
- 无移动结构
- 仅在给定位置插入,需要大量迭代,处理效率低且复杂
- 没有front((和back((运算符
- 。。。。更多
如果您的链表使用符合stl的接口,那么您可以更容易地实现该功能。仅作为C++20的示例:
#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;
}